sinlog 0.0.4 → 0.0.6

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/rbi/sinlog.rbi ADDED
@@ -0,0 +1,95 @@
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinlog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - 2moe
@@ -14,20 +14,19 @@ executables: []
14
14
  extensions: []
15
15
  extra_rdoc_files: []
16
16
  files:
17
+ - ".rubocop.yml"
17
18
  - License
18
19
  - docs/Readme-zh.md
19
20
  - docs/Readme.md
20
21
  - lib/sinlog.rb
21
- - lib/sinlog/init.rb
22
+ - lib/sinlog/consts.rb
22
23
  - lib/sinlog/log_ext.rb
23
- - lib/sinlog/log_short_ext.rb
24
24
  - lib/sinlog/loggable.rb
25
+ - lib/sinlog/logger.rb
26
+ - lib/sinlog/module_fn.rb
27
+ - lib/sinlog/short_ext.rb
25
28
  - lib/sinlog/version.rb
26
- - rbi/sinlog/init.rbi
27
- - rbi/sinlog/log_ext.rbi
28
- - rbi/sinlog/log_short_ext.rbi
29
- - rbi/sinlog/loggable.rbi
30
- - rbi/sinlog/version.rbi
29
+ - rbi/sinlog.rbi
31
30
  homepage: https://github.com/2moe/sinlog-gem
32
31
  licenses:
33
32
  - MIT
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,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
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 Loggleable
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)