sinlog 0.0.5 → 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.
data/lib/sinlog/init.rb DELETED
@@ -1,106 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'singleton'
4
- require 'logger'
5
-
6
- # Logger Singleton Class
7
- class Sinlog
8
- include Singleton
9
- attr_reader :logger
10
-
11
- # Define colors for different log levels
12
- COLORS = {
13
- debug: "\e[34m", # Blue
14
- info: "\e[36m", # Cyan
15
- warn: "\e[33m", # Yellow
16
- error: "\e[31m", # Red
17
- fatal: "\e[35m", # Magenta
18
- unknown: "\e[0m" # Reset
19
- }.freeze
20
-
21
- # log levels
22
- LV = {
23
- debug: Logger::DEBUG,
24
- info: Logger::INFO,
25
- warn: Logger::WARN,
26
- error: Logger::ERROR,
27
- fatal: Logger::FATAL,
28
- unknown: Logger::UNKNOWN
29
- }.freeze
30
-
31
- # Example:
32
- #
33
- # logger = Sinlog.instance.logger
34
- # logger.info "Information"
35
- # logger.debug "This is a debug message"
36
- #
37
- # The log output format will be similar to:
38
- #
39
- # [INFO] 21:29:22.004 Information
40
- # [DEBUG] 21:29:22.005 This is a debug message
41
- #
42
- # Where "INFO" is highlighted in cyan and "DEBUG" is highlighted in blue.
43
- #
44
- # The default log level is set based on the RUBY_LOG environment variable.
45
- # If this variable is not set, the default level is DEBUG.
46
- def initialize
47
- @logger = Logger.new($stderr)
48
- set_level_from_env!
49
- @logger.formatter = proc do |severity, datetime, progname, msg|
50
- color = COLORS[severity.downcase.to_sym]
51
- reset = COLORS[:unknown]
52
- formatted_datetime = datetime.strftime('%H:%M:%S.%L')
53
- prog = format_prog_name(progname)
54
- "[#{color}#{severity}#{reset}] #{formatted_datetime} #{prog}#{msg}\n"
55
- end
56
- end
57
-
58
- def self.logger
59
- instance.logger
60
- end
61
-
62
- # Example:
63
- #
64
- # require 'sinlog'
65
- # dbg_logger = Sinlog.with_level(Sinlog::LV[:debug])
66
- # dbg_logger.info "This is an info message"
67
- # dbg_logger.debug "This is a debug message"
68
- def self.logger_with_level(level = LV[:warn])
69
- instance.logger.tap { _1.level = level }
70
- end
71
-
72
- # Set the `@logger.level` (**log level**) based on the value of an environment variable.
73
- #
74
- # If env_name is not specified, it reads the value of the `RUBY_LOG` environment variable.
75
- #
76
- # - If the value exists, it is converted to lowercase, then to a symbol, and looked up in the LV hash;
77
- # - If it does not exist, the default level is DEBUG;
78
- # - If the lookup result is invalid, the level is set to UNKNOWN;
79
- # - If the environment variable value is empty, the lookup result will be invalid,
80
- # and the level will be set to UNKNOWN.
81
- #
82
- # Example:
83
- #
84
- # ENV["XX_LOG"] = "info" # or setenv in posix-sh: export XX_LOG=info
85
- # logger = Sinlog.instance.tap { it.set_level_from_env!("XX_LOG") }.logger
86
- #
87
- # logger.debug "This message will not be displayed because the current log level is info"
88
- # logger.info "Hello!"
89
- def set_level_from_env!(env_name = 'RUBY_LOG')
90
- env_lv = ENV[env_name]&.downcase&.to_sym || :debug
91
-
92
- (LV[env_lv] || Logger::UNKNOWN)
93
- .then { @logger.level = _1 }
94
- end
95
-
96
- private
97
-
98
- def format_prog_name(progname)
99
- return '' if progname.to_s.empty?
100
-
101
- green = "\e[32m"
102
- reset = "\e[0m"
103
- space = ' '
104
- "<#{green}#{progname}#{reset}>#{space}"
105
- end
106
- end
@@ -1,62 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Sinlog
4
- # LogExt is a Refinement that adds convenient logging methods to all objects.
5
- #
6
- # When activated with `using Sinlog::LogExt`, any object can call:
7
- #
8
- # == Overview
9
- #
10
- # * `log_dbg` – Logs the object at DEBUG level
11
- # * `log_info` – Logs the object at INFO level
12
- # * `log_warn` – Logs the object at WARN level
13
- # * `log_err` – Logs the object at ERROR level
14
- # * `log_fatal` – Logs the object at FATAL level
15
- # * `log_unk` – Logs the object at UNKNOWN level
16
- #
17
- # == Example
18
- #
19
- # require 'sinlog'
20
- #
21
- # class A
22
- # using Sinlog::LogExt
23
- # def demo
24
- # "Something happened".log_warn
25
- # end
26
- # end
27
- #
28
- module LogExt
29
- refine Object do
30
- # Logs the current object at *debug* level using Sinlog.logger
31
- def log_dbg
32
- Sinlog.logger.debug(self)
33
- end
34
-
35
- # Logs the current object at *information* level using Sinlog.logger
36
- def log_info
37
- Sinlog.logger.info(self)
38
- end
39
-
40
- # Logs the current object at *warning* level using Sinlog.logger
41
- def log_warn
42
- Sinlog.logger.warn(self)
43
- end
44
-
45
- # Logs the current object at *error* level using Sinlog.logger
46
- def log_err
47
- Sinlog.logger.error(self)
48
- end
49
-
50
- # Logs the current object at *fatal* level using Sinlog.logger
51
- def log_fatal
52
- Sinlog.logger.fatal(self)
53
- end
54
-
55
- # Logs the current object at *unknown* level using Sinlog.logger
56
- def log_unk
57
- Sinlog.logger.unknown(self)
58
- end
59
- # -----
60
- end
61
- end
62
- end
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Sinlog
4
- # LogShortExt is a Refinement that adds convenient logging methods to all objects.
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
- # When activated with `using Sinlog::LogShortExt`, any object can call:
13
- #
14
- # == Overview
15
- #
16
- # * `dbg` – Logs the object at DEBUG level
17
- # * `info` – Logs the object at INFO level
18
- # * `warn` – Logs the object at WARN level
19
- # * `err` – Logs the object at ERROR level
20
- # * `fatal` – Logs the object at FATAL level
21
- # * `unk` – Logs the object at UNKNOWN level
22
- #
23
- # == Example
24
- #
25
- # require 'sinlog'
26
- #
27
- # class A
28
- # using Sinlog::LogShortExt
29
- #
30
- # def demo
31
- # "Something happened".warn
32
- # end
33
- # end
34
- #
35
- module LogShortExt
36
- # Logs the current object at *debug* level using Sinlog.logger
37
- refine Object do
38
- def dbg
39
- Sinlog.logger.debug(self)
40
- end
41
-
42
- # Logs the current object at *information* level using Sinlog.logger
43
- def info
44
- Sinlog.logger.info(self)
45
- end
46
-
47
- # Logs the current object at *warning* level using Sinlog.logger
48
- def warn
49
- Sinlog.logger.warn(self)
50
- end
51
-
52
- # Logs the current object at *error* level using Sinlog.logger
53
- def err
54
- Sinlog.logger.error(self)
55
- end
56
-
57
- # Logs the current object at *fatal* level using Sinlog.logger
58
- def fatal
59
- Sinlog.logger.fatal(self)
60
- end
61
-
62
- # Logs the current object at *unknown* level using Sinlog.logger
63
- def unk
64
- Sinlog.logger.unknown(self)
65
- end
66
- # -----
67
- end
68
- end
69
- end
@@ -1,57 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Sinlog
4
- # Provides logging helpers for any object.
5
- #
6
- # == Overview
7
- #
8
- # * `log_dbg` – Logs the object at DEBUG level
9
- # * `log_info` – Logs the object at INFO level
10
- # * `log_warn` – Logs the object at WARN level
11
- # * `log_err` – Logs the object at ERROR level
12
- # * `log_fatal` – Logs the object at FATAL level
13
- # * `log_unk` – Logs the object at UNKNOWN level
14
- #
15
- # == Example
16
- #
17
- # require 'sinlog'
18
- #
19
- # include Sinlog::Loggable
20
- #
21
- # "A debug message".log_dbg
22
- # "Hello, world".log_info
23
- #
24
- module Loggable
25
- # Logs the current object at *debug* level using Sinlog.logger
26
- def log_dbg
27
- Sinlog.logger.debug(self)
28
- end
29
-
30
- # Logs the current object at *information* level using Sinlog.logger
31
- # logger.info
32
- def log_info
33
- Sinlog.logger.info(self)
34
- end
35
-
36
- # Logs the current object at *warning* level using Sinlog.logger
37
- def log_warn
38
- Sinlog.logger.warn(self)
39
- end
40
-
41
- # Logs the current object at *error* level using Sinlog.logger
42
- def log_err
43
- Sinlog.logger.error(self)
44
- end
45
-
46
- # Logs the current object at *fatal* level using Sinlog.logger
47
- def log_fatal
48
- Sinlog.logger.fatal(self)
49
- end
50
-
51
- # Logs the current object at *unknown* level using Sinlog.logger
52
- def log_unk
53
- Sinlog.logger.unknown(self)
54
- end
55
- # -----
56
- end
57
- end
data/rbi/sinlog/init.rbi DELETED
@@ -1,34 +0,0 @@
1
- # rubocop:disable Style/FrozenStringLiteralComment
2
- # rubocop:disable Style/Documentation
3
- # typed: true
4
-
5
- class Sinlog
6
- include ::Singleton::SingletonInstanceMethods
7
- include ::Singleton
8
- extend ::Singleton::SingletonClassMethods
9
-
10
- sig { params(level: Integer).returns(Logger) }
11
- def self.logger_with_level(level = LV[:warn]); end
12
-
13
- sig { returns(Logger) }
14
- def self.logger; end
15
-
16
- sig { returns(Logger) }
17
- def logger; end
18
-
19
- sig { params(env_name: String).returns(NilClass) }
20
- def set_level_from_env!(env_name = ''); end
21
-
22
- private
23
-
24
- sig { params(progname: T.any(String, Symbol)).returns(String) }
25
- def format_prog_name(progname); end
26
- end
27
-
28
- Sinlog::COLORS = T.let(T.unsafe(nil), T::Hash[Symbol, String])
29
-
30
- Sinlog::LV = T.let(T.unsafe(nil), T::Hash[Symbol, Integer])
31
-
32
- # ---------
33
- # LambdaExt
34
- # LoggerFn = T.type_alias { T.proc.params(msg: T.untyped).void }
@@ -1,27 +0,0 @@
1
- # rubocop:disable Style/FrozenStringLiteralComment
2
- # rubocop:disable Style/Documentation
3
- # typed: true
4
-
5
- class Sinlog
6
- module LogExt
7
- refine Object do
8
- sig { void }
9
- def log_dbg; end
10
-
11
- sig { void }
12
- def log_info; end
13
-
14
- sig { void }
15
- def log_warn; end
16
-
17
- sig { void }
18
- def log_err; end
19
-
20
- sig { void }
21
- def log_fatal; end
22
-
23
- sig { void }
24
- def log_unk; end
25
- end
26
- end
27
- end
@@ -1,27 +0,0 @@
1
- # rubocop:disable Style/FrozenStringLiteralComment
2
- # rubocop:disable Style/Documentation
3
- # typed: true
4
-
5
- class Sinlog
6
- module LogShortExt
7
- refine Object do
8
- sig { void }
9
- def dbg; end
10
-
11
- sig { void }
12
- def info; end
13
-
14
- sig { void }
15
- def warn; end
16
-
17
- sig { void }
18
- def err; end
19
-
20
- sig { void }
21
- def fatal; end
22
-
23
- sig { void }
24
- def unk; end
25
- end
26
- end
27
- end
@@ -1,25 +0,0 @@
1
- # rubocop:disable Style/FrozenStringLiteralComment
2
- # rubocop:disable Style/Documentation
3
- # typed: true
4
-
5
- class Sinlog
6
- module Loggable
7
- sig { void }
8
- def log_dbg; end
9
-
10
- sig { void }
11
- def log_info; end
12
-
13
- sig { void }
14
- def log_warn; end
15
-
16
- sig { void }
17
- def log_err; end
18
-
19
- sig { void }
20
- def log_fatal; end
21
-
22
- sig { void }
23
- def log_unk; end
24
- end
25
- end
@@ -1,3 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- Sinlog::VERSION = T.let(T.unsafe(nil), String)