sinlog 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/docs/Changelog.md +40 -0
- data/docs/Readme-zh.md +106 -47
- data/docs/Readme.md +127 -76
- data/lib/sinlog/{consts.rb → 01_consts.rb} +2 -2
- data/lib/sinlog/02_logger.rb +109 -0
- data/lib/sinlog/{module_fn.rb → 03_module_fn.rb} +42 -26
- data/lib/sinlog/04_log_ext.rb +41 -0
- data/lib/sinlog/05_short_ext.rb +52 -0
- data/lib/sinlog/{loggable.rb → 06_loggable.rb} +58 -51
- data/lib/sinlog/07_proc.rb +93 -0
- data/lib/sinlog/08_module_short_ext.rb +89 -0
- data/lib/sinlog/version.rb +1 -1
- data/lib/sinlog.rb +8 -6
- metadata +10 -9
- data/.rubocop.yml +0 -33
- data/lib/sinlog/log_ext.rb +0 -46
- data/lib/sinlog/logger.rb +0 -78
- data/lib/sinlog/short_ext.rb +0 -55
- data/rbi/sinlog.rbi +0 -95
data/.rubocop.yml
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
# ln ~/.config/rubocop/config.yml .rubocop.yml
|
|
2
|
-
AllCops:
|
|
3
|
-
TargetRubyVersion: 3.1
|
|
4
|
-
|
|
5
|
-
Lint/MissingCopEnableDirective:
|
|
6
|
-
Enabled: false
|
|
7
|
-
Lint/RedundantCopDisableDirective:
|
|
8
|
-
Enabled: false
|
|
9
|
-
Lint/EmptyExpression:
|
|
10
|
-
# disabled => allow ()
|
|
11
|
-
Enabled: false
|
|
12
|
-
|
|
13
|
-
# https://docs.rubocop.org/rubocop/cops_style.html
|
|
14
|
-
Style/TrailingCommaInHashLiteral:
|
|
15
|
-
EnforcedStyle: diff_comma
|
|
16
|
-
Style/TrailingCommaInArrayLiteral:
|
|
17
|
-
EnforcedStyle: diff_comma
|
|
18
|
-
Style/Lambda:
|
|
19
|
-
EnforcedStyle: literal
|
|
20
|
-
Style/ModuleFunction:
|
|
21
|
-
# EnforcedStyle: extend_self
|
|
22
|
-
EnforcedStyle: module_function
|
|
23
|
-
Style/BlockDelimiters:
|
|
24
|
-
Enabled: false
|
|
25
|
-
Style/Documentation:
|
|
26
|
-
Enabled: false
|
|
27
|
-
|
|
28
|
-
# https://docs.rubocop.org/rubocop/cops_layout.html
|
|
29
|
-
Layout/CaseIndentation:
|
|
30
|
-
EnforcedStyle: end
|
|
31
|
-
IndentOneStep: true
|
|
32
|
-
Layout/MultilineMethodCallIndentation:
|
|
33
|
-
EnforcedStyle: indented_relative_to_receiver
|
data/lib/sinlog/log_ext.rb
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Sinlog
|
|
4
|
-
# == Overview
|
|
5
|
-
#
|
|
6
|
-
# * `log_dbg` – DEBUG
|
|
7
|
-
# * `log_info` – INFO
|
|
8
|
-
# * `log_warn` – WARN
|
|
9
|
-
# * `log_err` – ERROR
|
|
10
|
-
# * `log_fatal` – FATAL
|
|
11
|
-
# * `log_unk` – UNKNOWN
|
|
12
|
-
#
|
|
13
|
-
module LogExt
|
|
14
|
-
# Logs the current object at *debug* level using Sinlog.logger
|
|
15
|
-
def log_dbg
|
|
16
|
-
Sinlog.logger.debug(self)
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
# Logs the current object at *information* level using Sinlog.logger
|
|
20
|
-
def log_info
|
|
21
|
-
Sinlog.logger.info(self)
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
# Logs the current object at *warning* level using Sinlog.logger
|
|
25
|
-
def log_warn
|
|
26
|
-
Sinlog.logger.warn(self)
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
# Logs the current object at *error* level using Sinlog.logger
|
|
30
|
-
def log_err
|
|
31
|
-
Sinlog.logger.error(self)
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
# Logs the current object at *fatal* level using Sinlog.logger
|
|
35
|
-
def log_fatal
|
|
36
|
-
Sinlog.logger.fatal(self)
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
# Logs the current object at *unknown* level using Sinlog.logger
|
|
40
|
-
def log_unk
|
|
41
|
-
Sinlog.logger.unknown(self)
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
# -----
|
|
45
|
-
private_constant :LogExt
|
|
46
|
-
end
|
data/lib/sinlog/logger.rb
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
# Logger Singleton Class
|
|
4
|
-
module Sinlog
|
|
5
|
-
class Logger
|
|
6
|
-
require 'singleton'
|
|
7
|
-
|
|
8
|
-
include Singleton
|
|
9
|
-
attr_reader :logger
|
|
10
|
-
|
|
11
|
-
# Example:
|
|
12
|
-
#
|
|
13
|
-
# logger = Sinlog::Logger.instance.logger
|
|
14
|
-
# logger.info "Information"
|
|
15
|
-
# logger.debug "This is a debug message"
|
|
16
|
-
#
|
|
17
|
-
# The log output format will be similar to:
|
|
18
|
-
#
|
|
19
|
-
# [INFO] 21:29:22.004 Information
|
|
20
|
-
# [DEBUG] 21:29:22.005 This is a debug message
|
|
21
|
-
#
|
|
22
|
-
# Where "INFO" is highlighted in cyan and "DEBUG" is highlighted in blue.
|
|
23
|
-
#
|
|
24
|
-
# The default log level is set based on the RUBY_LOG environment variable.
|
|
25
|
-
# If this variable is not set, the default level is DEBUG.
|
|
26
|
-
def initialize
|
|
27
|
-
@logger = StdLogger.new($stderr)
|
|
28
|
-
set_level_from_env!
|
|
29
|
-
@logger.formatter = Kernel.proc do |severity, datetime, progname, msg|
|
|
30
|
-
color = COLORS[severity.downcase.to_sym]
|
|
31
|
-
reset = COLORS[:unknown]
|
|
32
|
-
formatted_datetime = datetime.strftime('%H:%M:%S.%L')
|
|
33
|
-
prog = format_prog_name(progname)
|
|
34
|
-
"[#{color}#{severity}#{reset}] #{formatted_datetime} #{prog}#{msg}\n"
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def self.logger
|
|
39
|
-
instance.logger
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
# Set the `@logger.level` (**log level**) based on the value of an environment variable.
|
|
43
|
-
#
|
|
44
|
-
# If env_name is not specified, it reads the value of the `RUBY_LOG` environment variable.
|
|
45
|
-
#
|
|
46
|
-
# - If the value exists, it is converted to lowercase, then to a symbol, and looked up in the LV hash;
|
|
47
|
-
# - If it does not exist, the default level is DEBUG(0);
|
|
48
|
-
# - If the lookup result is invalid, the level is set to ERROR(3);
|
|
49
|
-
# - If the environment variable value is empty, the lookup result will be invalid,
|
|
50
|
-
# and the level will be set to ERROR(3).
|
|
51
|
-
#
|
|
52
|
-
# Example:
|
|
53
|
-
#
|
|
54
|
-
# ENV["XX_LOG"] = "info" # or setenv in posix-sh: export XX_LOG=info
|
|
55
|
-
# logger = Sinlog::Logger.instance.tap { it.set_level_from_env!("XX_LOG") }.logger
|
|
56
|
-
#
|
|
57
|
-
# logger.debug "This message will not be displayed because the current log level is info"
|
|
58
|
-
# logger.info "Hello!"
|
|
59
|
-
def set_level_from_env!(env_name = 'RUBY_LOG')
|
|
60
|
-
env_str = ENV[env_name]&.downcase || 'debug'
|
|
61
|
-
|
|
62
|
-
Sinlog
|
|
63
|
-
.to_log_level(env_str)
|
|
64
|
-
.then { @logger.level = _1 }
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
private
|
|
68
|
-
|
|
69
|
-
def format_prog_name(progname)
|
|
70
|
-
return '' if progname.to_s.empty?
|
|
71
|
-
|
|
72
|
-
green = "\e[32m"
|
|
73
|
-
reset = "\e[0m"
|
|
74
|
-
space = ' '
|
|
75
|
-
"<#{green}#{progname}#{reset}>#{space}"
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
end
|
data/lib/sinlog/short_ext.rb
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Sinlog
|
|
4
|
-
# LogShortExt is a module that adds convenient logging methods.
|
|
5
|
-
#
|
|
6
|
-
# Similar to LogExt, but methods omit the `log_` prefix.
|
|
7
|
-
#
|
|
8
|
-
# For example:
|
|
9
|
-
# * `"msg".err` instead of `"msg".log_err`;
|
|
10
|
-
# * `"msg".warn` instead of `"msg".log_warn`
|
|
11
|
-
#
|
|
12
|
-
# We can activate it with `using Sinlog::ShortRefin`
|
|
13
|
-
#
|
|
14
|
-
# == Overview
|
|
15
|
-
#
|
|
16
|
-
# * `dbg` – DEBUG
|
|
17
|
-
# * `info` – INFO
|
|
18
|
-
# * `warn` – WARN
|
|
19
|
-
# * `err` – ERROR
|
|
20
|
-
# * `fatal` – FATAL
|
|
21
|
-
# * `unk` – UNKNOWN
|
|
22
|
-
module LogShortExt
|
|
23
|
-
# Logs the current object at *debug* level using Sinlog.logger
|
|
24
|
-
def dbg
|
|
25
|
-
Sinlog.logger.debug(self)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
# Logs the current object at *information* level using Sinlog.logger
|
|
29
|
-
def info
|
|
30
|
-
Sinlog.logger.info(self)
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
# Logs the current object at *warning* level using Sinlog.logger
|
|
34
|
-
def warn
|
|
35
|
-
Sinlog.logger.warn(self)
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
# Logs the current object at *error* level using Sinlog.logger
|
|
39
|
-
def err
|
|
40
|
-
Sinlog.logger.error(self)
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
# Logs the current object at *fatal* level using Sinlog.logger
|
|
44
|
-
def fatal
|
|
45
|
-
Sinlog.logger.fatal(self)
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
# Logs the current object at *unknown* level using Sinlog.logger
|
|
49
|
-
def unk
|
|
50
|
-
Sinlog.logger.unknown(self)
|
|
51
|
-
end
|
|
52
|
-
# -----
|
|
53
|
-
end
|
|
54
|
-
private_constant :LogShortExt
|
|
55
|
-
end
|
data/rbi/sinlog.rbi
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
# rubocop:disable Style/FrozenStringLiteralComment
|
|
2
|
-
# rubocop:disable Style/Documentation
|
|
3
|
-
# typed: true
|
|
4
|
-
|
|
5
|
-
module Sinlog
|
|
6
|
-
class Logger
|
|
7
|
-
include ::Singleton::SingletonInstanceMethods
|
|
8
|
-
include ::Singleton
|
|
9
|
-
extend ::Singleton::SingletonClassMethods
|
|
10
|
-
|
|
11
|
-
sig { params(level: T.nilable(Integer)).returns(::Logger) }
|
|
12
|
-
def self.logger_with_level(level = LV[:warn]); end
|
|
13
|
-
|
|
14
|
-
sig { returns(::Logger) }
|
|
15
|
-
def self.logger; end
|
|
16
|
-
|
|
17
|
-
sig { returns(::Logger) }
|
|
18
|
-
def logger; end
|
|
19
|
-
|
|
20
|
-
sig { params(env_name: String).returns(NilClass) }
|
|
21
|
-
def set_level_from_env!(env_name = ''); end
|
|
22
|
-
|
|
23
|
-
private
|
|
24
|
-
|
|
25
|
-
sig { params(progname: T.any(String, Symbol)).returns(String) }
|
|
26
|
-
def format_prog_name(progname); end
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
Sinlog::COLORS = T.let(T.unsafe(nil), T::Hash[Symbol, String])
|
|
31
|
-
Sinlog::LV = T.let(T.unsafe(nil), T::Hash[Symbol, Integer])
|
|
32
|
-
|
|
33
|
-
# ---------
|
|
34
|
-
# LambdaExt
|
|
35
|
-
# LoggerFn = T.type_alias { T.proc.params(msg: T.untyped).void }
|
|
36
|
-
|
|
37
|
-
# rubocop:disable Style/FrozenStringLiteralComment
|
|
38
|
-
# rubocop:disable Style/Documentation
|
|
39
|
-
# typed: true
|
|
40
|
-
|
|
41
|
-
class Object
|
|
42
|
-
sig { void }
|
|
43
|
-
def log_dbg; end
|
|
44
|
-
|
|
45
|
-
sig { void }
|
|
46
|
-
def log_info; end
|
|
47
|
-
|
|
48
|
-
sig { void }
|
|
49
|
-
def log_warn; end
|
|
50
|
-
|
|
51
|
-
sig { void }
|
|
52
|
-
def log_err; end
|
|
53
|
-
|
|
54
|
-
sig { void }
|
|
55
|
-
def log_fatal; end
|
|
56
|
-
|
|
57
|
-
sig { void }
|
|
58
|
-
def log_unk; end
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
class Object
|
|
62
|
-
sig { void }
|
|
63
|
-
def dbg; end
|
|
64
|
-
|
|
65
|
-
sig { void }
|
|
66
|
-
def info; end
|
|
67
|
-
|
|
68
|
-
sig { void }
|
|
69
|
-
def warn; end
|
|
70
|
-
|
|
71
|
-
sig { void }
|
|
72
|
-
def err; end
|
|
73
|
-
|
|
74
|
-
sig { void }
|
|
75
|
-
def fatal; end
|
|
76
|
-
|
|
77
|
-
sig { void }
|
|
78
|
-
def unk; end
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
# -----
|
|
82
|
-
LevelType = T.type_alias { T.nilable(T.any(String, Integer, Symbol)) }
|
|
83
|
-
|
|
84
|
-
module Sinlog
|
|
85
|
-
module_function # rubocop:disable Lint/UselessAccessModifier,Style/ModuleFunction
|
|
86
|
-
|
|
87
|
-
sig { params(level: LevelType).returns(T.untyped) }
|
|
88
|
-
def to_log_level(level); end
|
|
89
|
-
|
|
90
|
-
sig { returns(Sinlog::Logger) }
|
|
91
|
-
def instance; end
|
|
92
|
-
|
|
93
|
-
sig { params(env_name: T.nilable(String), level: LevelType).returns(::Logger) }
|
|
94
|
-
def logger(env_name: nil, level: nil); end
|
|
95
|
-
end
|