lokilogger 0.6.0 → 1.1.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
  SHA256:
3
- metadata.gz: 2b658e3b80bf4ab3cc950ab3d9112398a2572a0be682319b7f5ee5561e95427f
4
- data.tar.gz: 7be0372d1f05446788643cc62564a932364bfb942b8eabb5acc37584891bcd6e
3
+ metadata.gz: beca007a56461abf51d521051073e9514f30ce6834b9d2a0f1cf10a0016242c9
4
+ data.tar.gz: 9ffee2c539de6dc7803a93538452e24639f5a2110a33d8c059bdc44e2004c160
5
5
  SHA512:
6
- metadata.gz: dc550875c2e5ba7930c1c87282be7f14e28c9262f7838333246c9670374d5b45f733869bfc12bd225e965260f19258b79e7d71cbaf1320e7bd095b1beadb4799
7
- data.tar.gz: e881dfa29a1a2fb8dc44904d2059a75bf7d0b1465880551dc35fb7215c746800af9ff269d6fc6bd6f780d8a7f3197e0c167c00609b899805ad4dab17af97986e
6
+ metadata.gz: c65d8d472ead0c1d655acdaf876e65e15e5f3f89c90471a3b4519b975751ddb6b3df1d8c63c6e0964fc228d918397dec10e427722318a227a43b94688129877c
7
+ data.tar.gz: dc681934d3d5a921bec4631574006d9765d073ac5f3a136641a16d69093881e9e5c7f536cefe8c8236ff1bce888d9f711b0a8fa21c8369d875702974788cba47
data/README.adoc CHANGED
@@ -61,13 +61,13 @@ Start by creating a new logger instance:
61
61
  ----
62
62
  # required: url, log_level, version and tags
63
63
  # optional: username and password (if one of them is nil, no auth will be used)
64
- logger = Lokilogger.new({url:, log_level: 0, version: "v1", username:, password:, tags: {foo: "bar"}})
64
+ logger = Lokilogger::Logger.new({url:, log_level: 0, version: "v1", username:, password:, tags: {foo: "bar"}})
65
65
  ----
66
66
 
67
67
  If you need proxy or custom TLS settings, you can optionally pass as well:
68
68
 
69
69
  - ssl_options (link:https://lostisland.github.io/faraday/#/customization/ssl-options[Faraday docs on ssl options])
70
- - proxy_options (link:https://lostisland.github.io/faraday/#/customization/proxy-options[Farady docs on proxy options])
70
+ - proxy_options (link:https://lostisland.github.io/faraday/#/customization/proxy-options[Faraday docs on proxy options])
71
71
 
72
72
  then it's straight forward:
73
73
 
@@ -17,9 +17,7 @@ module Lokilogger
17
17
 
18
18
  Faraday.default_adapter = :async_http
19
19
  @conn = Faraday.new url: @config[:url], headers: {"Content-Type" => "application/json"}, ssl: ssl_options, proxy: proxy_options, request: request_options do |builder|
20
- if @config[:username] && @config[:password]
21
- builder.request :authorization, :basic, @config[:username], @config[:password]
22
- end
20
+ builder.request :authorization, :basic, @config[:username], @config[:password] if @config[:username] && @config[:password]
23
21
  end
24
22
  end
25
23
 
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lokilogger
4
+ # Default formatter for log messages.
5
+ class Formatter
6
+ Format = "%.1s, [%s #%d] %5s -- %s: %s\n"
7
+ DatetimeFormat = "%Y-%m-%dT%H:%M:%S.%6N"
8
+
9
+ attr_accessor :datetime_format
10
+
11
+ def initialize
12
+ @datetime_format = nil
13
+ end
14
+
15
+ def call severity, time, progname, msg
16
+ format Format, severity, format_datetime(time), Process.pid, severity, progname, msg2str(msg)
17
+ end
18
+
19
+ private
20
+
21
+ def format_datetime time
22
+ time.strftime(@datetime_format || DatetimeFormat)
23
+ end
24
+
25
+ def msg2str msg
26
+ case msg
27
+ when ::String
28
+ msg
29
+ when ::Exception
30
+ "#{msg.message} (#{msg.class})\n#{msg.backtrace.join "\n" if msg.backtrace}"
31
+ else
32
+ msg.inspect
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lokilogger
4
+ # Logging severity.
5
+ module Severity
6
+ # Low-level information, mostly for developers.
7
+ DEBUG = 0
8
+ # Generic (useful) information about system operation.
9
+ INFO = 1
10
+ # A warning.
11
+ WARN = 2
12
+ # A handleable error condition.
13
+ ERROR = 3
14
+ # An unhandleable error that results in a program crash.
15
+ FATAL = 4
16
+ # An unknown message that should always be logged.
17
+ UNKNOWN = 5
18
+
19
+ LEVELS = {"debug" => DEBUG, "info" => INFO, "warn" => WARN, "error" => ERROR, "fatal" => FATAL, "unknown" => UNKNOWN}.freeze
20
+ private_constant :LEVELS
21
+
22
+ def self.coerce severity
23
+ if severity.is_a? Integer
24
+ severity
25
+ else
26
+ key = severity.to_s.downcase
27
+ LEVELS[key] || fail(ArgumentError, "invalid log level: #{severity}")
28
+ end
29
+ end
30
+ end
31
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Main namespace.
4
4
  module Lokilogger
5
- VERSION = "0.6.0"
5
+ Version = "1.1.0"
6
6
  end
data/lib/lokilogger.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "lokilogger/formatter"
4
+ require "lokilogger/severity"
3
5
  require "zeitwerk"
4
6
 
5
7
  Zeitwerk::Loader.new.then do |loader|
@@ -14,77 +16,109 @@ module Lokilogger
14
16
  @loader ||= registry.loaders.find { |loader| loader.tag == File.basename(__FILE__, ".rb") }
15
17
  end
16
18
 
17
- def self.new config
18
- @client = Lokilogger::Client.new config
19
- self
20
- end
19
+ class Logger
20
+ include Severity
21
21
 
22
- def self.debug(*args)
23
- message "debug", args
24
- end
22
+ def initialize config
23
+ @client = Lokilogger::Client.new config
24
+ @default_formatter = Formatter.new
25
+ end
25
26
 
26
- def self.debug?
27
- true
28
- end
27
+ def datetime_format= datetime_format
28
+ @default_formatter.datetime_format = datetime_format
29
+ end
29
30
 
30
- def self.info(*args)
31
- message "info", args
32
- end
31
+ def datetime_format
32
+ @default_formatter.datetime_format
33
+ end
33
34
 
34
- def self.info?
35
- true
36
- end
35
+ attr_accessor :formatter
37
36
 
38
- def self.warn(*args)
39
- message "warn", args
40
- end
37
+ def debug(*args)
38
+ message "debug", args
39
+ end
41
40
 
42
- def self.warn?
43
- true
44
- end
41
+ def debug?
42
+ @client.log_level <= DEBUG
43
+ end
45
44
 
46
- def self.error(*args)
47
- message "error", args
48
- end
45
+ def debug!
46
+ @client.log_level = DEBUG
47
+ end
49
48
 
50
- def self.error?
51
- true
52
- end
49
+ def info(*args)
50
+ message "info", args
51
+ end
53
52
 
54
- def self.fatal(*args)
55
- message "fatal", args
56
- end
53
+ def info?
54
+ @client.log_level <= INFO
55
+ end
57
56
 
58
- def self.fatal?
59
- true
60
- end
57
+ def info!
58
+ @client.log_level = INFO
59
+ end
61
60
 
62
- def self.unknown(*args)
63
- message "unknown", args
64
- end
61
+ def warn(*args)
62
+ message "warn", args
63
+ end
65
64
 
66
- def self.unknown?
67
- true
68
- end
65
+ def warn?
66
+ @client.log_level <= WARN
67
+ end
69
68
 
70
- def self.level= level
71
- @client.log_level = level
72
- end
69
+ def warn!
70
+ @client.log_level = WARN
71
+ end
73
72
 
74
- def self.level
75
- @client.log_level
76
- end
73
+ def error(*args)
74
+ message "error", args
75
+ end
77
76
 
78
- def self.formatter
79
- end
77
+ def error?
78
+ @client.log_level <= ERROR
79
+ end
80
80
 
81
- def self.formatter=(*args)
82
- end
81
+ def error!
82
+ @client.log_level = ERROR
83
+ end
84
+
85
+ def fatal(*args)
86
+ message "fatal", args
87
+ end
88
+
89
+ def fatal?
90
+ @client.log_level <= FATAL
91
+ end
92
+
93
+ def fatal!
94
+ @client.log_level = FATAL
95
+ end
96
+
97
+ def unknown(*args)
98
+ message "unknown", args
99
+ end
100
+
101
+ def unknown?
102
+ @client.log_level <= UNKNOWN
103
+ end
104
+
105
+ def unknown!
106
+ @client.log_level = UNKNOWN
107
+ end
108
+
109
+ def level= severity
110
+ @client.log_level = Severity.coerce severity
111
+ end
112
+
113
+ def level
114
+ @client.log_level
115
+ end
83
116
 
84
- def self.message(severity, *args)
85
- message = args[0][0]
86
- extra_tags = args[0][1] if args[0].count > 1
117
+ def message(severity, *args)
118
+ message = args[0][0]
119
+ extra_tags = args[0][1] if args[0].count > 1
87
120
 
88
- @client.request severity, message, extra_tags
121
+ @client.request severity, message, extra_tags
122
+ end
89
123
  end
90
124
  end
data/lokilogger.gemspec CHANGED
@@ -6,7 +6,7 @@ require "lokilogger/version"
6
6
 
7
7
  Gem::Specification.new do |spec|
8
8
  spec.name = "lokilogger"
9
- spec.version = Lokilogger::VERSION
9
+ spec.version = Lokilogger::Version
10
10
  spec.authors = ["Nils Bartels"]
11
11
  spec.email = ["rubygems@nils.bartels.xyz"]
12
12
  spec.homepage = "https://gitlab.com/sukunai/lokilogger"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lokilogger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nils Bartels
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-03 00:00:00.000000000 Z
11
+ date: 2023-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async
@@ -106,6 +106,8 @@ files:
106
106
  - lib/lokilogger.rb
107
107
  - lib/lokilogger/client.rb
108
108
  - lib/lokilogger/error.rb
109
+ - lib/lokilogger/formatter.rb
110
+ - lib/lokilogger/severity.rb
109
111
  - lib/lokilogger/version.rb
110
112
  - lokilogger.gemspec
111
113
  homepage: https://gitlab.com/sukunai/lokilogger