relaton-logger 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +12 -0
- data/LICENSE.txt +21 -0
- data/README.adoc +132 -0
- data/Rakefile +12 -0
- data/lib/relaton/logger/config.rb +34 -0
- data/lib/relaton/logger/formatter_json.rb +8 -0
- data/lib/relaton/logger/formatter_string.rb +12 -0
- data/lib/relaton/logger/log.rb +64 -0
- data/lib/relaton/logger/log_device.rb +27 -0
- data/lib/relaton/logger/pool.rb +24 -0
- data/lib/relaton/logger/version.rb +7 -0
- data/lib/relaton/logger.rb +17 -0
- data/sig/relaton/logger.rbs +6 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2da06b3c702a902ef2fc5110c19a2c1f0c997caa3409a2c014fa2b8968ec0873
|
4
|
+
data.tar.gz: 7908c829ec6d272cc924d0ae64a100f13b473945f0bca7496679215f515ccb8a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6b484c092d45bc81bab1c0551f48f81df0b64eb1f23eea52fa341f1fc1a8893264ee03e3de18a9af2f056e316a86d6002e9227e98555c80357d922465cc8d81d
|
7
|
+
data.tar.gz: cbae4869240542f537409d49c05864de5ca358fc17698fab793c794e7a2338cb8673524e450aebc9e1ea27adc7416c05912622bf6f92bd23e4ff684887554b80
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# This project follows the Ribose OSS style guide.
|
2
|
+
# https://github.com/riboseinc/oss-guides
|
3
|
+
# All project-specific additions and overrides should be specified in this file.
|
4
|
+
|
5
|
+
require: rubocop-rails
|
6
|
+
|
7
|
+
inherit_from:
|
8
|
+
- https://raw.githubusercontent.com/riboseinc/oss-guides/master/ci/rubocop.yml
|
9
|
+
AllCops:
|
10
|
+
TargetRubyVersion: 2.7
|
11
|
+
Rails:
|
12
|
+
Enabled: false
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Andrei Kislichenko
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.adoc
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
= Relaton::Logger
|
2
|
+
|
3
|
+
image:https://img.shields.io/gem/v/relaton-logger.svg["Gem Version", link="https://rubygems.org/gems/relaton-logger"]
|
4
|
+
image:https://github.com/relaton/relaton-logger/workflows/rake/badge.svg["Build Status", link="https://github.com/relaton/relaton-logger/actions?workflow=rake"]
|
5
|
+
image:https://codeclimate.com/github/relaton/relaton-logger/badges/gpa.svg["Code Climate", link="https://codeclimate.com/github/relaton/relaton-logger"]
|
6
|
+
image:https://img.shields.io/github/issues-pr-raw/relaton/relaton-logger.svg["Pull Requests", link="https://github.com/relaton/relaton-logger/pulls"]
|
7
|
+
image:https://img.shields.io/github/commits-since/relaton/relaton-logger/latest.svg["Commits since latest",link="https://github.com/relaton/relaton-logger/releases"]
|
8
|
+
|
9
|
+
Relaton::Logger is a Ruby gem that implements a logger for Relaton gems. It is also possible to use this gem as a standalone logger.
|
10
|
+
|
11
|
+
== Installation
|
12
|
+
|
13
|
+
Install the gem and add to the application's Gemfile by executing:
|
14
|
+
|
15
|
+
$ bundle add relaton-logger
|
16
|
+
|
17
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
18
|
+
|
19
|
+
$ gem install relaton-logger
|
20
|
+
|
21
|
+
== Usage
|
22
|
+
|
23
|
+
The Relaton::Logger gem provides a logger pool that can be used to log messages. The logger pool is a singleton and can be accessed by calling `Relaton.logger_pool`. The logger pool is an Array that contains loggers. By default, the logger pool contains a single logger, which is an instance of `Relaton::Logger::Log` class, which inherits the `Logger` class from the Ruby standard library.
|
24
|
+
|
25
|
+
The logger pool can be used to log messages by calling the methods of the loggers in the pool. These methods are the same as the methods of the `Logger` class from the Ruby standard library (`debug`, `info`, `warn`, `error`, `fatal`, `unknown`).
|
26
|
+
|
27
|
+
[source, ruby]
|
28
|
+
----
|
29
|
+
require "relaton/logger"
|
30
|
+
|
31
|
+
Relaton.logger_pool.info "message"
|
32
|
+
INFO: message
|
33
|
+
----
|
34
|
+
|
35
|
+
The program name and the key can be passed as arguments to the log methods. The program name is a string that is added to the log message. The key is a string that is added to the log message in parentheses.
|
36
|
+
|
37
|
+
[source, ruby]
|
38
|
+
----
|
39
|
+
Relaton.logger_pool.warn "message", "progname", key: "KEY"
|
40
|
+
[progname] WARN: (KEY) message
|
41
|
+
----
|
42
|
+
|
43
|
+
The block form of the log methods can be used to log messages that are expensive to generate. The block is called only if the message is logged.
|
44
|
+
|
45
|
+
[source, ruby]
|
46
|
+
----
|
47
|
+
Relaton.logger_pool.error("progname", key: "KEY") { "message" }
|
48
|
+
[progname] ERROR: (KEY) message
|
49
|
+
----
|
50
|
+
|
51
|
+
The logger pool can configured to contain multiple loggers. The loggers in the pool are called in the order they are added to the pool. To add a logger to the pool, call the `<<` method of the logger pool and pass the logger as an argument.
|
52
|
+
|
53
|
+
[source, ruby]
|
54
|
+
----
|
55
|
+
Relaton::Logger.configure do |config|
|
56
|
+
config.logger_pool << Relaton::Logger::Log.new("relaton.log", levels: [:info, :warn])
|
57
|
+
end
|
58
|
+
----
|
59
|
+
|
60
|
+
To replace the loggers in the pool, use `=` method of the logger pool and pass the new loggers as an argument.
|
61
|
+
|
62
|
+
[source, ruby]
|
63
|
+
----
|
64
|
+
Relaton::Logger.configure do |config|
|
65
|
+
config.logger_pool = [Relaton::Logger::Log.new("relaton.log", levels: [:info, :warn])]
|
66
|
+
end
|
67
|
+
----
|
68
|
+
|
69
|
+
To create a new logger, call the `new` method of the `Relaton::Logger::Log` class and pass arguments:
|
70
|
+
|
71
|
+
- `logdev` - file name or an IO object. If nil or File::NULL, messages are not logged. If it is a file name, the file is opened in append mode. If it is an IO object, messages are written to it.
|
72
|
+
- `shift_age` - the number of old log files to keep. If 0, no old log files are kept. If it is a number, old log files are kept and the log file is rotated when it reaches the maximum size. If it is a string, old log files are kept and the log file is rotated when it reaches the maximum size. The string is a time unit and a number, for example, "daily 7" or "weekly 4". Default is 0.
|
73
|
+
- `shift_size` - the maximum size of the log file. If it is a number, the log file is rotated when it reaches the maximum size. If it is a string, the log file is rotated when it reaches the maximum size. The string is a number and a time unit, for example, "1048576" or "1M". Default is 1048576.
|
74
|
+
- `levels` - an array of symbols that represent the log levels The log levels are `:debug`, `:info`, `:warn`, `:error`, `:fatal`, `:unknown`. Default is `[:info, :warn, :error, :fatal, :unknown]`.
|
75
|
+
- `formatter` - a formatter object that is used to format the log messages. Two formatters are available at this moment `Relaton::Logger::FormatterString`, `Relaton::Logger::FormatterJson`, and `Proc`. The default formater is `Relaton::Logger::FormatterString`.
|
76
|
+
- `progname` - a string that is added to the log message. Default is nil.
|
77
|
+
|
78
|
+
[source, ruby]
|
79
|
+
----
|
80
|
+
Relaton::Logger::Log.new("relaton.log", levels: [:info, :warn], formatter: Relaton::Logger::FormatterJSON, progname: "progname")
|
81
|
+
----
|
82
|
+
|
83
|
+
To create a custom formatter implement a class with the `call` method which takes agruments:
|
84
|
+
|
85
|
+
- `severity` - the log level
|
86
|
+
- `time` - the time the message was logged
|
87
|
+
- `progname` - the program name
|
88
|
+
- `msg` - the message
|
89
|
+
- any other keyword arguments that are passed to the log method, for example, `key`
|
90
|
+
|
91
|
+
[source, ruby]
|
92
|
+
----
|
93
|
+
class CustomFormatter
|
94
|
+
def call(severity, time, progname, msg, key: "Key 1")
|
95
|
+
"#{time} [#{severity}] #{progname} (#{key}): #{msg}\n"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
Relaton::Logger::Log.new("relaton.log", formatter: CustomFormatter)
|
100
|
+
----
|
101
|
+
|
102
|
+
It's possible to use `Proc` as a formatter. The `Proc` object is called with the same arguments as the `call` method of the custom formatter.
|
103
|
+
|
104
|
+
[source, ruby]
|
105
|
+
----
|
106
|
+
log = Relaton::Logger::Log.new("relaton.log")
|
107
|
+
|
108
|
+
log.formatter = Proc.new do |severity, time, progname, msg, key: "Key 1"|
|
109
|
+
"#{time} [#{severity}] #{progname} (#{key}): #{msg}\n"
|
110
|
+
end
|
111
|
+
----
|
112
|
+
|
113
|
+
A log file can be cleared by calling the `truncate` method of the logger pool.
|
114
|
+
|
115
|
+
[source, ruby]
|
116
|
+
----
|
117
|
+
Relaton.logger_pool.truncate
|
118
|
+
----
|
119
|
+
|
120
|
+
== Development
|
121
|
+
|
122
|
+
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.
|
123
|
+
|
124
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
125
|
+
|
126
|
+
== Contributing
|
127
|
+
|
128
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/relaton/relaton-logger.
|
129
|
+
|
130
|
+
== License
|
131
|
+
|
132
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module Relaton::Logger
|
2
|
+
module Config
|
3
|
+
def configure
|
4
|
+
yield configuration if block_given?
|
5
|
+
end
|
6
|
+
|
7
|
+
def configuration
|
8
|
+
@configuration ||= self::Configuration.new
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Configuration
|
13
|
+
# @return [Array<Relaton::Logger::Log>] List of loggers
|
14
|
+
attr_reader :logger_pool
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
@logger_pool ||= Pool.new
|
18
|
+
@logger_pool << Log.new($stderr, levels: %i[info warn error fatal])
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# Replace the current list of loggers with the given list of loggers.
|
23
|
+
#
|
24
|
+
# @param [Array<Relaton::Logger::Log>] loggers list of loggers
|
25
|
+
#
|
26
|
+
# @return [void]
|
27
|
+
#
|
28
|
+
def logger_pool=(loggers)
|
29
|
+
@logger_pool.loggers = loggers
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
extend Config
|
34
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Relaton::Logger
|
2
|
+
class FormatterString < ::Logger::Formatter
|
3
|
+
def call(severity, _datetime, progname, msg, **args)
|
4
|
+
output = []
|
5
|
+
output << "[#{progname}]" if progname
|
6
|
+
output << "#{severity}:"
|
7
|
+
output << "(#{args[:key]})" if args[:key]
|
8
|
+
output << "#{msg}\n"
|
9
|
+
output.join " "
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module Relaton::Logger
|
2
|
+
class Log < ::Logger
|
3
|
+
attr_reader :levels
|
4
|
+
|
5
|
+
def initialize(logdev, shift_age = 0, shift_size = 1_048_576, **args) # rubocop:disable Lint/MissingSuper
|
6
|
+
self.levels = args.delete(:levels) || [UNKNOWN, FATAL, ERROR, WARN, INFO]
|
7
|
+
self.level = @levels.min
|
8
|
+
self.progname = args.delete(:progname)
|
9
|
+
@default_formatter = FormatterString.new
|
10
|
+
self.datetime_format = args.delete(:datetime_format)
|
11
|
+
self.formatter = args.delete(:formatter)
|
12
|
+
self.formatter = self.formatter.new if self.formatter.is_a? Class
|
13
|
+
@logdev = nil
|
14
|
+
@level_override = {}
|
15
|
+
if logdev && logdev != File::NULL
|
16
|
+
@logdev = LogDevice.new(logdev, shift_age: shift_age, shift_size: shift_size, **args)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def levels=(levels)
|
21
|
+
@levels = Set.new levels.map { |l| Severity.coerce l }
|
22
|
+
self.level = @levels.min
|
23
|
+
end
|
24
|
+
|
25
|
+
def add_level(level)
|
26
|
+
@levels << Severity.coerce(level)
|
27
|
+
self.level = @levels.min
|
28
|
+
end
|
29
|
+
|
30
|
+
def remove_level(level)
|
31
|
+
@levels.delete Severity.coerce(level)
|
32
|
+
self.level = @levels.min
|
33
|
+
end
|
34
|
+
|
35
|
+
def unknown(message = nil, progname = nil, **args, &block)
|
36
|
+
level = Object.const_get "Logger::#{__callee__.to_s.upcase}"
|
37
|
+
add(level, message, progname, **args, &block)
|
38
|
+
end
|
39
|
+
|
40
|
+
%i[fatal error warn info debug].each { |m| alias_method m, :unknown }
|
41
|
+
|
42
|
+
def add(severity, message = nil, progname = nil, **args) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
|
43
|
+
severity ||= UNKNOWN
|
44
|
+
return true if @logdev.nil? || !@levels.include?(severity)
|
45
|
+
|
46
|
+
if block_given?
|
47
|
+
progname ||= message || @progname
|
48
|
+
message = yield
|
49
|
+
else
|
50
|
+
progname ||= @progname
|
51
|
+
end
|
52
|
+
@logdev.write format_message(format_severity(severity), Time.now, progname, message, **args)
|
53
|
+
true
|
54
|
+
end
|
55
|
+
|
56
|
+
def format_message(severity, datetime, progname, msg, **args)
|
57
|
+
(@formatter || @default_formatter).call(severity, datetime, progname, msg, **args)
|
58
|
+
end
|
59
|
+
|
60
|
+
def truncate
|
61
|
+
@logdev.truncate
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Relaton::Logger
|
4
|
+
class LogDevice < ::Logger::LogDevice
|
5
|
+
#
|
6
|
+
# @param header [Boolean, nil] whether to add header to log file
|
7
|
+
#
|
8
|
+
def initialize(logdev, **args)
|
9
|
+
# TODO: the header is not used yet, maybe it will be used in the future for not JSON formatters
|
10
|
+
@header = args.delete :header
|
11
|
+
super
|
12
|
+
end
|
13
|
+
|
14
|
+
def add_log_header(file)
|
15
|
+
return unless @header
|
16
|
+
|
17
|
+
super
|
18
|
+
end
|
19
|
+
|
20
|
+
def truncate
|
21
|
+
return unless @dev.respond_to? :truncate
|
22
|
+
|
23
|
+
@dev.truncate 0
|
24
|
+
@dev.rewind
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Relaton::Logger
|
2
|
+
class Pool
|
3
|
+
extend Forwardable
|
4
|
+
def_delegators :@loggers, :<<, :[], :first, :last, :empty?, :any?, :size, :each, :detect, :length
|
5
|
+
|
6
|
+
attr_accessor :loggers
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@loggers = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def unknown(message = nil, progname = nil, **args, &block)
|
13
|
+
@loggers.each { |logger| logger.send(__callee__, message, progname, **args, &block) }
|
14
|
+
nil
|
15
|
+
end
|
16
|
+
|
17
|
+
%i[fatal error warn info debug].each { |m| alias_method m, :unknown }
|
18
|
+
|
19
|
+
def truncate
|
20
|
+
@loggers.each(&:truncate)
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "forwardable"
|
4
|
+
require "logger"
|
5
|
+
require_relative "logger/version"
|
6
|
+
require_relative "logger/log"
|
7
|
+
require_relative "logger/log_device"
|
8
|
+
require_relative "logger/pool"
|
9
|
+
require_relative "logger/formatter_string"
|
10
|
+
require_relative "logger/formatter_json"
|
11
|
+
require_relative "logger/config"
|
12
|
+
|
13
|
+
module Relaton
|
14
|
+
def self.logger_pool
|
15
|
+
Logger.configuration.logger_pool
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: relaton-logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ribose Inc.
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-03-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: logger
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.6.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.6.0
|
27
|
+
description: The logger for Relaton gem.
|
28
|
+
email:
|
29
|
+
- open.source@ribose.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".rspec"
|
35
|
+
- ".rubocop.yml"
|
36
|
+
- LICENSE.txt
|
37
|
+
- README.adoc
|
38
|
+
- Rakefile
|
39
|
+
- lib/relaton/logger.rb
|
40
|
+
- lib/relaton/logger/config.rb
|
41
|
+
- lib/relaton/logger/formatter_json.rb
|
42
|
+
- lib/relaton/logger/formatter_string.rb
|
43
|
+
- lib/relaton/logger/log.rb
|
44
|
+
- lib/relaton/logger/log_device.rb
|
45
|
+
- lib/relaton/logger/pool.rb
|
46
|
+
- lib/relaton/logger/version.rb
|
47
|
+
- sig/relaton/logger.rbs
|
48
|
+
homepage: https://github.com/relaton/relaton-logger
|
49
|
+
licenses:
|
50
|
+
- MIT
|
51
|
+
metadata:
|
52
|
+
homepage_uri: https://github.com/relaton/relaton-logger
|
53
|
+
source_code_uri: https://github.com/relaton/relaton-logger
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 2.7.0
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubygems_version: 3.3.26
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: The logger for Relaton gem.
|
73
|
+
test_files: []
|