journald-logger 1.1.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 62d2d53d4a1407b0920b2c2be1d0df05b1f54ab9
4
- data.tar.gz: 710e877b8743df26f72ba87dafde4471f442b6a2
3
+ metadata.gz: eaa7f70e9a09a6429c672b60ba973d4f9d545c06
4
+ data.tar.gz: b01df4422dd13e47c001eddeaafe05398ded8ac7
5
5
  SHA512:
6
- metadata.gz: 4cbbb86b7a317ee62d44cbcdc0e13c1659b57cde072609ffbd27e94867237f0f23b36b8a11a6f03133067b5d26b886c391c12aec1f4e1a51ead9d3bc083552dd
7
- data.tar.gz: 3eafb8f19e5fcbe74827798ee9a4a81c895e00c014cba99274093296fca5c3cec13535cbec2b52b756d1d58d07ea6eb8d4565105a66818c27b107bf8b7dfd53c
6
+ metadata.gz: ba24c66aceca488ab25698c9db07716a5116cb1556de8d9b449f8bbd924f729941e7a09712527ff9eea2ec79dc748e4814f6b813d7172fee64089124a67480c5
7
+ data.tar.gz: bbdcde2b188bb1cea50809dac1d4c4aaf563be3a9b3f15380c86845588e9d13b74d8095994ba4ffe56af7233a8251fdd4c76e19d15399adb1c5b1acee566fd04
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014 Anton Smirnov
1
+ Copyright (c) 2014-2015 Anton Smirnov
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -56,12 +56,14 @@ Tags are used to add systemd-journal fields to all subsequent log calls until re
56
56
 
57
57
  ```ruby
58
58
  logger = Journald::Logger.new('gandalf', world: 'arda') # set world tag in costructor
59
- logger.tag :location, 'moria' # add/replace location
60
- logger.tag(:object, 'balrog') do # use object field in the block
61
- # log as 'MESSAGE=you shall not pass!', 'PRIORITY=4', 'LOCATION=moria', 'OBJECT=balrog', 'WORLD=arda'
59
+ logger.tag location: 'shire', weapon: 'staff' # add/replace location and weapon
60
+ logger.tag(location: 'moria', object: 'balrog') do # change location and use object in the block
61
+ # log as 'MESSAGE=you shall not pass!', 'PRIORITY=4', 'LOCATION=moria', 'OBJECT=balrog', 'WORLD=arda', 'WEAPON=staff'
62
62
  logger.warn 'you shall not pass!'
63
- end
64
- logger.untag :location # remove location
63
+ end # return location & object to the previous state
64
+ # log as 'MESSAGE=That was not in canon!', 'PRIORITY=6', 'LOCATION=shire', 'WORLD=arda', 'WEAPON=staff'
65
+ logger.info 'That was not in canon!'
66
+ logger.untag :location, :weapon # remove location and weapon
65
67
  ```
66
68
 
67
69
  Tag names must follow systemd-journal fields naming convention:
@@ -72,11 +74,11 @@ letters, numbers, underscores, cannot begin with underscore. Library upcases all
72
74
  Two methods which look similarly to native systemd-journal api
73
75
 
74
76
  ```ruby
75
- logger.send({
77
+ logger.send(
76
78
  message: 'hi!',
77
79
  priority: Journald::LOG_NOTICE,
78
80
  any_field: 'any_value',
79
- }) # tags will be added here
81
+ ) # tags will be added here
80
82
  logger.print Journald::LOG_NOTICE, 'hi!' # and here
81
83
  ```
82
84
 
@@ -107,11 +109,11 @@ Exception logger automatically fills the following fields:
107
109
  EXCEPTION_CLASS=ExceptionRealClassName
108
110
  EXCEPTION_MESSAGE=Original exception message
109
111
  BACKTRACE=full backtrace
110
- CAUSE=exception cause (Ruby >= 2.1)
112
+ CAUSE=exception cause
111
113
  GEM_LOGGER_MESSAGE_TYPE=Exception
112
114
  ```
113
115
 
114
- In Ruby 2.1 it also tries to log ```CODE_LINE```, ```CODE_FILE``` and ```CODE_FUNC``` and try to recurse into Cause and log it into a separate message with ```GEM_LOGGER_MESSAGE_TYPE=ExceptionCause```
116
+ It also tries to log ```CODE_LINE```, ```CODE_FILE``` and ```CODE_FUNC``` and try to recurse into Cause and log it into a separate message with ```GEM_LOGGER_MESSAGE_TYPE=ExceptionCause```
115
117
 
116
118
  ## License
117
119
 
@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
16
16
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
17
  spec.require_paths = ['lib']
18
18
 
19
- spec.required_ruby_version = '>= 2.0.0'
19
+ spec.required_ruby_version = '>= 2.1.0'
20
20
 
21
21
  spec.add_runtime_dependency 'journald-native', '~> 1.0'
22
22
 
@@ -4,17 +4,7 @@ module Journald
4
4
  include Loggable
5
5
  include Sysloggable
6
6
 
7
- def initialize(progname = nil, min_priority = nil, tags = {})
8
- if progname.is_a? Hash
9
- tags = progname
10
- progname = min_priority = nil
11
- end
12
-
13
- if min_priority.is_a? Hash
14
- tags = min_priority
15
- min_priority = nil
16
- end
17
-
7
+ def initialize(progname = nil, min_priority = nil, **tags)
18
8
  @tags = tags
19
9
  @logger = Native
20
10
  self.min_priority = min_priority
@@ -26,7 +16,7 @@ module Journald
26
16
  end
27
17
 
28
18
  def progname=(value)
29
- tag(:syslog_identifier, value)
19
+ tag(syslog_identifier: value)
30
20
  end
31
21
 
32
22
  attr_reader :min_priority
@@ -52,13 +42,22 @@ module Journald
52
42
 
53
43
  # add tags
54
44
 
55
- # add tag to all log messages
56
- def tag(key, value)
57
- @tags[key] = value
45
+ # add tags to all log messages
46
+ def tag(**tags)
47
+ values = {}
48
+ if block_given?
49
+ # remember old values
50
+ values = tag_values(*tags.keys)
51
+ end
52
+
53
+ tags.each do |key, value|
54
+ @tags[key] = value
55
+ end
58
56
 
59
57
  if block_given?
60
58
  yield
61
- untag(key)
59
+ # restore old values
60
+ tag(values)
62
61
  end
63
62
  end
64
63
 
@@ -67,29 +66,37 @@ module Journald
67
66
  @tags[key]
68
67
  end
69
68
 
69
+ # get tag values
70
+ # return everything including nil for non-set
71
+ def tag_values(*keys)
72
+ keys.inject({}) { |hash, key| hash[key] = @tags[key]; hash }
73
+ end
74
+
70
75
  # stop adding the tag
71
- def untag(key)
72
- @tags.delete(key)
76
+ def untag(*keys)
77
+ keys.each do |key|
78
+ @tags.delete(key)
79
+ end
73
80
  end
74
81
 
75
82
  protected
76
83
 
77
84
  # used internally by exception() and TraceLogger
78
85
  def tag_trace_location(location)
79
- tag :code_file, location.path
80
- tag :code_line, location.lineno
81
- tag :code_func, location.label
86
+ values = tag_values(:code_file, :code_line, :code_func)
87
+
88
+ tag code_file: location.path,
89
+ code_line: location.lineno,
90
+ code_func: location.label
82
91
 
83
92
  if block_given?
84
93
  yield
85
- untag_trace_location
94
+ tag(values)
86
95
  end
87
96
  end
88
97
 
89
98
  def untag_trace_location
90
- untag :code_file
91
- untag :code_line
92
- untag :code_func
99
+ untag :code_file, :code_line, :code_func
93
100
  end
94
101
 
95
102
  private
@@ -1,6 +1,6 @@
1
1
  module Journald
2
2
  class TraceLogger
3
- def initialize(progname = nil, min_priority = nil, tags = {})
3
+ def initialize(progname = nil, min_priority = nil, **tags)
4
4
  @wrapped_logger = ::Journald::Logger.new(progname, min_priority, tags)
5
5
  end
6
6
 
@@ -38,7 +38,4 @@ module Journald
38
38
  end
39
39
  end
40
40
  end
41
-
42
- # alias for the old class name
43
- TracerLogger = TraceLogger
44
41
  end
@@ -9,11 +9,8 @@ module Journald
9
9
  private
10
10
 
11
11
  def real_exception(e, priority, is_cause)
12
- # for Ruby 2.1 get cause if present
13
- cause = if e.respond_to? :cause; e.cause; end
14
- # for Ruby 2.1 get backtrace if present
15
- bt = e.respond_to?(:backtrace_locations) &&
16
- e.backtrace_locations &&
12
+ # get backtrace if present
13
+ bt = e.backtrace_locations &&
17
14
  e.backtrace_locations.length > 0
18
15
 
19
16
  tag_trace_location(e.backtrace_locations[0]) if bt
@@ -24,13 +21,13 @@ module Journald
24
21
  gem_logger_message_type: is_cause ? 'ExceptionCause' : 'Exception',
25
22
  exception_class: e.class.name,
26
23
  exception_message: e.message,
27
- backtrace: e.backtrace.join("\n"),
28
- cause: cause ? cause.inspect : nil,
24
+ backtrace: bt ? e.backtrace.join("\n"): nil,
25
+ cause: e.cause ? e.cause.inspect : nil,
29
26
  })
30
27
 
31
28
  untag_trace_location if bt
32
29
 
33
- real_exception(cause, priority, true) if cause
30
+ real_exception(e.cause, priority, true) if e.cause
34
31
  end
35
32
  end
36
33
  end
@@ -1,5 +1,5 @@
1
1
  module Journald
2
2
  class Logger
3
- VERSION = '1.1.1'
3
+ VERSION = '2.0.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: journald-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Smirnov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-18 00:00:00.000000000 Z
11
+ date: 2015-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: journald-native
@@ -84,7 +84,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
84
84
  requirements:
85
85
  - - ">="
86
86
  - !ruby/object:Gem::Version
87
- version: 2.0.0
87
+ version: 2.1.0
88
88
  required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  requirements:
90
90
  - - ">="
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
92
  version: '0'
93
93
  requirements: []
94
94
  rubyforge_project:
95
- rubygems_version: 2.4.3
95
+ rubygems_version: 2.4.8
96
96
  signing_key:
97
97
  specification_version: 4
98
98
  summary: systemd-journal native logger