Skip to content

Commit

Permalink
Update logger (#88)
Browse files Browse the repository at this point in the history
Switch to using relaton-logger to log messages
  • Loading branch information
andrew2net authored Jul 3, 2024
1 parent 2859229 commit a4ecc16
Show file tree
Hide file tree
Showing 27 changed files with 50 additions and 78 deletions.
17 changes: 5 additions & 12 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,13 @@ Or install it yourself as:

== Usage

=== Configuration

Configuration is optional. The available option is `logger` which is a `Logger` instance. By default, the logger is `Logger.new($stderr)` with `Logger::WARN` level. To change the logger level, use `RelatonBib.configure` block.
=== Create bibliographic item

[source,ruby]
----
require 'relaton_bib'
=> true
RelatonBib.configure do |config|
config.logger.level = Logger::DEBUG
end
----

=== Create bibliographic item

[source,ruby]
----
hash = YAML.load_file "spec/examples/bib_item.yml"
=> {"id"=>"ISOTC211",
"fetched"=>"2022-05-02",
Expand Down Expand Up @@ -304,6 +293,10 @@ item.to_bibxml
</reference>"
----

=== Logging

RelatonBib uses the relaton-logger gem for logging. By default, it logs to STDOUT. To change the log levels and add other loggers, read the https://github.com/relaton/relaton-logger#usage[relaton-logger] documentation.

== Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
3 changes: 3 additions & 0 deletions grammars/basicdoc.rng
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@
<data type="ID"/>
</attribute>
<attribute name="reviewer"/>
<optional>
<attribute name="type"/>
</optional>
<optional>
<attribute name="date">
<data type="dateTime"/>
Expand Down
10 changes: 5 additions & 5 deletions grammars/versions.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"relaton-models": "v1.2.8",
"basicdoc-models": "v1.1.2",
"relaton-models": "v1.2.9",
"basicdoc-models": "v1.1.3",
"metanorma-requirements-models": "v1.0.1",
"relaton-model-ieee": "v1.0.1",
"relaton-model-iso": "v1.0.0",
"relaton-model-iso": "v1.0.3",
"relaton-model-iec": "v1.0.0",
"relaton-model-bsi": "v1.0.2",
"relaton-model-gb": "v1.0.0",
Expand All @@ -29,6 +29,6 @@
"relaton-model-oasis": "v1.0.1",
"relaton-model-jis": "v0.0.1",
"relaton-model-etsi": "v0.0.3",
"metanorma-model": "v1.2.11",
"date": "2024-01-04T03:55:06Z"
"metanorma-model": "v1.3.0",
"date": "2024-02-21T18:15:14Z"
}
2 changes: 1 addition & 1 deletion lib/relaton_bib.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "relaton/logger"
require "forwardable"
require "yaml"
require "htmlentities"
require "relaton_bib/version"
require "relaton_bib/deep_dup"
require "relaton_bib/config"
require "relaton_bib/util"
require "relaton_bib/localized_string"
require "relaton_bib/forename"
Expand Down
2 changes: 1 addition & 1 deletion lib/relaton_bib/bib_item_locality.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(type, reference_from, reference_to = nil)
whole|table|annex|figure|note|list|example|volume|issue|time|anchor|
locality:[a-zA-Z0-9_]+}x
unless type&.match? type_ptrn
Util.warn "WARNING: invalid locality type: `#{type}`"
Util.warn "Invalid locality type: `#{type}`"
end

@type = type
Expand Down
2 changes: 1 addition & 1 deletion lib/relaton_bib/bibliographic_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class BibliographicItem
# @option link [String] :content
def initialize(**args)
if args[:type] && !TYPES.include?(args[:type])
Util.warn %{WARNING: type `#{args[:type]}` is invalid.}
Util.warn %{Type `#{args[:type]}` is invalid.}
end

@title = if args[:title].is_a?(TypedTitleStringCollection)
Expand Down
12 changes: 0 additions & 12 deletions lib/relaton_bib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ def configuration
end

class Configuration
PROGNAME = "relaton-bib".freeze

attr_accessor :logger

def initialize
@logger = ::Logger.new $stderr
@logger.level = ::Logger::WARN
@logger.progname = self.class::PROGNAME
@logger.formatter = proc do |_severity, _datetime, progname, msg|
"[#{progname}] #{msg}\n"
end
end
end

extend Config
Expand Down
2 changes: 1 addition & 1 deletion lib/relaton_bib/contribution_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ContributorRole
# @param description [Array<String>]
def initialize(**args)
if args[:type] && !TYPES.include?(args[:type])
Util.warn %{WARNING: Contributor's type `#{args[:type]}` is invalid.}
Util.warn %{Contributor's type `#{args[:type]}` is invalid.}
end

@type = args[:type]
Expand Down
2 changes: 1 addition & 1 deletion lib/relaton_bib/document_relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def initialize(type:, bibitem:, description: nil, locality: [],
source_locality: [])
type = "obsoletes" if type == "Now withdrawn"
unless self.class::TYPES.include? type
Util.warn "WARNING: invalid relation type: `#{type}`"
Util.warn "Invalid relation type: `#{type}`"
end
@type = type
@description = description
Expand Down
2 changes: 1 addition & 1 deletion lib/relaton_bib/hash_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def relation_bibitem_hash_to_bib(rel)
if rel[:bibitem]
rel[:bibitem] = bib_item hash_to_bib(rel[:bibitem])
else
Util.warn "WARNING: bibitem missing: `#{rel}`"
Util.warn "Bibitem missing: `#{rel}`"
rel[:bibitem] = nil
end
end
Expand Down
13 changes: 6 additions & 7 deletions lib/relaton_bib/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ module RelatonBib
module Util
extend self

def method_missing(...)
logger.send(...)
end
PROGNAME = "relaton-bib".freeze

def respond_to_missing?(method_name, include_private = false)
logger.respond_to?(method_name) || super
def method_missing(method_name, msg = nil, prog = nil, **opts, &block)
prog ||= self::PROGNAME
Relaton.logger_pool.send method_name, msg, prog, **opts, &block
end

def logger
RelatonBib.configuration.logger
def respond_to_missing?(method_name, include_private = false)
Relaton.logger_pool.respond_to?(method_name) || super
end
end
end
2 changes: 1 addition & 1 deletion lib/relaton_bib/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RelatonBib
VERSION = "1.18.5".freeze
VERSION = "1.19.0".freeze
end
2 changes: 1 addition & 1 deletion lib/relaton_bib/xml_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def from_xml(xml)
if bibitem
bib_item item_data(bibitem)
else
Util.warn "WARNING: can't find bibitem or bibdata element in the XML"
Util.warn "Can't find bibitem or bibdata element in the XML"
nil
end
end
Expand Down
1 change: 1 addition & 0 deletions relaton-bib.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ Gem::Specification.new do |spec|
spec.add_dependency "htmlentities"
spec.add_dependency "iso639"
spec.add_dependency "nokogiri", "~> 1.16"
spec.add_dependency "relaton-logger", "~> 0.2.0"
end
4 changes: 2 additions & 2 deletions spec/examples/bib_item.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<bibitem id="ISOTC211" type="standard" schema-version="v1.2.8">
<bibitem id="ISOTC211" type="standard" schema-version="v1.2.9">
<fetched>2024-01-10</fetched>
<title type="title-main" format="text/plain">Geographic information</title>
<title type="main" format="text/plain">Geographic information</title>
Expand Down Expand Up @@ -288,4 +288,4 @@
<validityEnds>2011-02-03 18:30</validityEnds>
<revision>2011-03-04 09:00</revision>
</validity>
</bibitem>
</bibitem>
4 changes: 2 additions & 2 deletions spec/examples/bibdata_item.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<bibdata type="standard" schema-version="v1.2.8">
<bibdata type="standard" schema-version="v1.2.9">
<fetched>2024-01-10</fetched>
<title type="title-main" format="text/plain">Geographic information</title>
<title type="main" format="text/plain">Geographic information</title>
Expand Down Expand Up @@ -316,4 +316,4 @@
<docnumber>456</docnumber>
</structuredidentifier>
</ext>
</bibdata>
</bibdata>
4 changes: 2 additions & 2 deletions spec/examples/bibdata_item_fr.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<bibdata type="standard" schema-version="v1.2.8">
<bibdata type="standard" schema-version="v1.2.9">
<fetched>2024-01-10</fetched>
<title format="text/plain" language="fr" script="Latn">Information géographique</title>
<uri type="src" language="en" script="Latn">https://www.iso.org/standard/53798.html</uri>
Expand Down Expand Up @@ -311,4 +311,4 @@
<docnumber>456</docnumber>
</structuredidentifier>
</ext>
</bibdata>
</bibdata>
2 changes: 1 addition & 1 deletion spec/examples/from_bibtex.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<bibitem id="mrx05" type="article" schema-version="v1.2.8">
<bibitem id="mrx05" type="article" schema-version="v1.2.9">
<fetched>2019-12-05</fetched>
<title type="main" format="text/plain">Something Great</title>
<title type="main" format="text/plain">Sub title</title>
Expand Down
2 changes: 1 addition & 1 deletion spec/examples/hash.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
schema-version: v1.2.8
schema-version: v1.2.9
id: ISOTC211
title:
- content: Geographic information
Expand Down
2 changes: 1 addition & 1 deletion spec/relaton_bib/bib_item_locality_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
it "warn if locality type is invalid" do
expect do
RelatonBib::BibItemLocality.new "type", "from"
end.to output(/invalid locality type/).to_stderr
end.to output(/Invalid locality type/).to_stderr_from_any_process
end
end
6 changes: 2 additions & 4 deletions spec/relaton_bib/bibliographic_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
require "jing"

RSpec.describe "RelatonBib" => :BibliographicItem do
before(:each) { RelatonBib.instance_variable_set :@configuration, nil }

context "initialize" do
it "with keyword Hash" do
keyword = [{ content: "keyword", language: "en", script: "Latn" }]
Expand Down Expand Up @@ -336,8 +334,8 @@

it "warn invalid type argument error" do
expect { RelatonBib::BibliographicItem.new type: "type" }.to output(
/\[relaton-bib\] WARNING: type `type` is invalid./,
).to_stderr
/\[relaton-bib\] WARN: Type `type` is invalid./,
).to_stderr_from_any_process
end

context RelatonBib::CopyrightAssociation do
Expand Down
6 changes: 0 additions & 6 deletions spec/relaton_bib/config_spec.rb

This file was deleted.

6 changes: 1 addition & 5 deletions spec/relaton_bib/contribution_info_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
RSpec.describe RelatonBib::ContributorRole do
before(:each) do
RelatonBib.instance_variable_set :@configuration, nil
end

it "raises invalid type argument error" do
expect { RelatonBib::ContributorRole.new type: "type" }.to output(
/Contributor's type `type` is invalid/,
).to_stderr
).to_stderr_from_any_process
end
end
4 changes: 1 addition & 3 deletions spec/relaton_bib/document_relation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
RSpec.describe RelatonBib::DocumentRelation do
before(:each) { RelatonBib.instance_variable_set :@configuration, nil }

it "warn when type is invalid" do
expect do
RelatonBib::DocumentRelation.new type: "invalid", bibitem: nil
end.to output(/invalid relation type/).to_stderr
end.to output(/Invalid relation type/).to_stderr_from_any_process
end
end
4 changes: 1 addition & 3 deletions spec/relaton_bib/hash_converter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
RSpec.describe RelatonBib::HashConverter do
before(:each) { RelatonBib.instance_variable_set :@configuration, nil }

it "warn if bibitem missig" do
expect do
ret = { relation: [type: "updates"] }
RelatonBib::HashConverter.relation_bibitem_hash_to_bib ret[:relation][0]
end.to output(/bibitem missing/).to_stderr
end.to output(/Bibitem missing/).to_stderr_from_any_process
end

it "make affiliation description from string" do
Expand Down
6 changes: 6 additions & 0 deletions spec/relaton_bib/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
it "#respond_to_missing?" do
expect(described_class.respond_to?(:warn)).to be true
end

it "#method_missing" do
expect do
expect(described_class.warn("msg")).to be nil
end.to output(/\[relaton-bib\] WARN: msg\n/).to_stderr_from_any_process
end
end
6 changes: 2 additions & 4 deletions spec/relaton_bib/xml_parser_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
RSpec.describe RelatonBib::XMLParser do
before(:each) { RelatonBib.instance_variable_set :@configuration, nil }

it "creates item from xml" do
xml = File.read "spec/examples/bib_item.xml", encoding: "UTF-8"
item = RelatonBib::XMLParser.from_xml xml
Expand Down Expand Up @@ -110,8 +108,8 @@
it "warn if XML doesn't have bibitem or bibdata element" do
item = ""
expect { item = RelatonBib::XMLParser.from_xml "" }.to output(
/can't find bibitem/,
).to_stderr
/Can't find bibitem/,
).to_stderr_from_any_process
expect(item).to be_nil
end
end

0 comments on commit a4ecc16

Please sign in to comment.