versalog 1.6.0 → 2.0.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1985cdbce1b4b3bb3dda6573700f12da050ac8b31f5a350be72bc66d1fe9fff5
4
- data.tar.gz: d6b091088bb69c9f85c31da76bd1a3e474aa6b75c7952ef0ab6198f66fd9764d
3
+ metadata.gz: 7da24cda409be01bf9a61a1f84f8c59ea4cb4198c19d83c47d3c4c21b64e0979
4
+ data.tar.gz: 0054eb19a8580de46548c849c258a500a72cd9c7cb5ea2e5124fac9908b10f21
5
5
  SHA512:
6
- metadata.gz: 709fb02663f87fc5154b3c00784e3eda4ad0ee112bc88c78a469ac16ed9d7b98fbe3cf56c567783eed0d0188cb87951149c2f1be74d971ce6bed175929fefabc
7
- data.tar.gz: 6e2510c7dd6658813b26e2dce0c8157eb14c5388e668c2773945722bd2fc4272538f081ee9433381bf43b7bd4b7f8991cf111db767b79230f261a4ab088ea708
6
+ metadata.gz: 49b60b5e876078521820be4a9333feae58c9466e10048bbe6174ddf2a497b79150bb9358499d5fc8285f5f4f5bc8b460008c68738c4c7a1f1682de610a777e1e
7
+ data.tar.gz: 81bfe1cc0ec008f63d6fc2583efd324f2ecc7f7a12e09b1f9a957824ab7cd934a7187e265f0135deba8063311cdc59112001a22bb9007d3ed4de79cfd7539469
data/README.md CHANGED
@@ -1,40 +1,51 @@
1
- # What is VersaLog.rb?
2
-
3
- What is VersaLog.rb?
4
- VersaLog is a powerful and flexible logging library for Ruby.
5
- It supports everything from simple usage to advanced, highly customizable configurations to meet a wide range of needs.
6
-
7
- ## Installation
8
-
9
- ```
10
-
11
- ```
12
-
13
- ### Enum
14
-
15
- | Enum | Description |
16
- | ---------- | ---------------------------------------------------------------------------- |
17
- | `detailed` | Logs including execution time and log levels |
18
- | `file` | Logs with filename and line number |
19
- | `simple` | Simple and easy-to-read logs |
20
- | `simple2` | Simple and easy-to-read log format. The timestamp is automatically included. |
21
-
22
- ### Options
23
-
24
- | Options | Description |
25
- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
26
- | `show_file` | True : Display filename and line number (for simple and detailed modes) |
27
- | `show_tag` | True : Show self.tag if no explicit tag is provided |
28
- | `tag` | Default tag to use when show_tag is enabled |
29
- | `enable_all` | Shortcut to enable both show_file and show_tag |
30
- | `notice` | True : When an error or critical level log is output, a desktop notification (using plyer.notification) will be displayed. The notification includes the log level and message. |
31
- | `all_save` | True : When an error or critical level log is output, the log will be saved to a file. |
32
- | `save_levels` | A list of log levels to save. Defaults to ["INFO", "ERROR", "WARNING", "DEBUG", "CRITICAL"]. |
33
- | `silent` | True : Suppress standard output (print) |
34
- | `catch_exceptions` | True : Automatically catch unhandled exceptions and log them as critical |
35
-
36
- ## Log save
37
-
38
- ```
39
- [2025-08-06 04:10:36][INFO] : ok
1
+ # What is VersaLog.rb?
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/versalog.svg)](https://badge.fury.io/rb/versalog)
4
+ [![Downloads](https://img.shields.io/gem/dt/versalog.svg)](https://rubygems.org/gems/versalog)
5
+ [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.txt)
6
+ [![Ruby Version](https://img.shields.io/badge/ruby-%3E%3D%202.0.0-red.svg)](https://www.ruby-lang.org/)
7
+
8
+ What is VersaLog.rb?
9
+ VersaLog is a powerful and flexible logging library for Ruby.
10
+ It supports everything from simple usage to advanced, highly customizable configurations to meet a wide range of needs.
11
+
12
+ ## Support
13
+
14
+ Join our Discord server for support, questions, and community discussions:
15
+
16
+ [![Discord](https://img.shields.io/badge/Discord-Support%20Server-7289DA?style=flat&logo=discord)](https://discord.gg/9EAsByDV)
17
+
18
+ ## Installation
19
+
20
+ ```
21
+ gem install versalog
22
+ ```
23
+
24
+ ### Enum
25
+
26
+ | Enum | Description |
27
+ | ---------- | ---------------------------------------------------------------------------- |
28
+ | `detailed` | Logs including execution time and log levels |
29
+ | `file` | Logs with filename and line number |
30
+ | `simple` | Simple and easy-to-read logs |
31
+ | `simple2` | Simple and easy-to-read log format. The timestamp is automatically included. |
32
+
33
+ ### Options
34
+
35
+ | Options | Description |
36
+ | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
37
+ | `show_file` | True : Display filename and line number (for simple and detailed modes) |
38
+ | `show_tag` | True : Show self.tag if no explicit tag is provided |
39
+ | `tag` | Default tag to use when show_tag is enabled |
40
+ | `enable_all` | Shortcut to enable both show_file and show_tag |
41
+ | `notice` | True : When an error or critical level log is output, a desktop notification (using plyer.notification) will be displayed. The notification includes the log level and message. |
42
+ | `all_save` | True : When an error or critical level log is output, the log will be saved to a file. |
43
+ | `save_levels` | A list of log levels to save. Defaults to ["INFO", "ERROR", "WARNING", "DEBUG", "CRITICAL"]. |
44
+ | `silent` | True : Suppress standard output (print) |
45
+ | `catch_exceptions` | True : Automatically catch unhandled exceptions and log them as critical |
46
+
47
+ ## Log save
48
+
49
+ ```
50
+ [2025-08-06 04:10:36][INFO] : ok
40
51
  ```
@@ -0,0 +1,51 @@
1
+ require "versalog"
2
+ require "net/http"
3
+ require "json"
4
+ require "uri"
5
+
6
+ logger = Versalog::VersaLog.new(
7
+ enum: "detailed",
8
+ show_tag: true,
9
+ tag: "Request"
10
+ )
11
+
12
+ def main(logger)
13
+ api = "http://api.openweathermap.org/data/2.5/weather"
14
+
15
+ params = {
16
+ q: "location name",
17
+ appid: "api key",
18
+ units: "metric",
19
+ lang: "ja"
20
+ }
21
+
22
+ uri = URI(api)
23
+ uri.query = URI.encode_www_form(params)
24
+
25
+ res = Net::HTTP.get_response(uri)
26
+
27
+ if res.is_a?(Net::HTTPSuccess)
28
+ data = JSON.parse(res.body)
29
+
30
+ location_name = data["name"]
31
+ weather_description = data["weather"][0]["description"]
32
+ temperature = data["main"]["temp"]
33
+ humidity = data["main"]["humidity"]
34
+ pressure = data["main"]["pressure"]
35
+ wind_speed = data["wind"]["speed"]
36
+
37
+ logger.info("success")
38
+ msg = "< #{location_name}の天気予報 >\n\n> 天気\n・#{weather_description}\n\n> 気温\n・#{temperature}°C\n\n> 湿度\n・#{humidity}%\n\n> 気圧\n・#{pressure} hPa\n\n> 風速\n・#{wind_speed} m/s"
39
+ puts msg
40
+ else
41
+ logger.error("failed (status: #{res.code})")
42
+ end
43
+ rescue => e
44
+ logger.error("failed: #{e.class} #{e.message}")
45
+ end
46
+
47
+ if __FILE__ == $0
48
+ main(logger)
49
+ end
50
+
51
+
@@ -0,0 +1,37 @@
1
+ require "versalog"
2
+
3
+ log = Versalog::VersaLog.new(
4
+ enum: "detailed",
5
+ tag: "BATCH",
6
+ show_file: false,
7
+ show_tag: true,
8
+ enable_all: false,
9
+ notice: false,
10
+ all_save: true,
11
+ save_levels: ["INFO", "ERROR"],
12
+ silent: false
13
+ )
14
+
15
+ total_files = 3
16
+
17
+ log.info("Batch Start")
18
+
19
+ log.timer("Total Batch") do
20
+ (1..total_files).each do |file_index|
21
+
22
+ log.step("Processing file_#{file_index}.txt", file_index, total_files)
23
+
24
+ log.timer("file_#{file_index}.txt") do
25
+ total_lines = 10
26
+
27
+ (1..total_lines).each do |i|
28
+ sleep 0.1
29
+ log.progress("file_#{file_index}.txt", i, total_lines)
30
+ end
31
+ end
32
+
33
+ log.progress("Overall Progress", file_index, total_files)
34
+ end
35
+ end
36
+
37
+ log.info("Batch Finished")
data/image/logo.png ADDED
Binary file
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Versalog
4
- VERSION = "1.6.0"
4
+ VERSION = "2.0.0"
5
5
  end
data/lib/versalog.rb CHANGED
@@ -1,272 +1,308 @@
1
- # frozen_string_literal: true
2
-
3
- require "time"
4
- require "caller"
5
- require "ENV"
6
- require "ARGV"
7
- require "thread"
8
- require "win32/notification"
9
- require "date"
10
- require "fileutils"
11
-
12
- require_relative "versalog/version"
13
-
14
- module Versalog
15
- class VersaLog
16
- COLOURS = {
17
- "INFO": "\033[32m",
18
- "ERROR": "\033[31m",
19
- "WARNING": "\033[33m",
20
- "DEBUG": "\033[36m",
21
- "CRITICAL": "\033[35m",
22
- }.freeze
23
-
24
- SYMBOLS = {
25
- "INFO": "[+]",
26
- "ERROR": "[-]",
27
- "WARNING": "[!]",
28
- "DEBUG": "[D]",
29
- "CRITICAL": "[C]",
30
- }.freeze
31
-
32
- RESET = "\033[0m"
33
-
34
- VALID_MODES = ["simple", "simple2", "detailed", "file"].freeze
35
- VALID_SAVE_LEVELS = ["INFO", "ERROR", "WARNING", "DEBUG", "CRITICAL"].freeze
36
-
37
- def initialize(
38
- enum: "simple",
39
- tag: nil,
40
- show_file: false,
41
- show_tag: false,
42
- enable_all: false,
43
- notice: false,
44
- all_save: false,
45
- save_levels: nil,
46
- silent: false,
47
- catch_exceptions: false)
48
-
49
- if enable_all
50
- show_file = true
51
- show_tag = true
52
- notice = true
53
- all_save = true
54
- end
55
-
56
- @enum = enum.downcase
57
- @tag = tag
58
- @show_file = show_file
59
- @show_tag = show_tag
60
- @enable_all = enable_all
61
- @notice = notice
62
- @all_save = all_save
63
- @save_levels = save_levels
64
- @silent = silent
65
- @catch_exceptions = catch_exceptions
66
-
67
- @log_queue = Queue.new
68
- @worker_thread = Thread.new { _worker }
69
- @last_cleanup_date = nil
70
-
71
- unless VALID_MODES.include?(@enum)
72
- raise ArgumentError, "Invalid enum: #{@enum}"
73
- end
74
-
75
- if @all_save
76
- if @save_levels.nil?
77
- @save_levels = VALID_SAVE_LEVELS.dup
78
- elsif !@save_levels.is_a?(Array)
79
- raise ArgumentError, "save_levels must be an Array. Example: ['ERROR']"
80
- elsif !@save_levels.all? { |level| VALID_SAVE_LEVELS.include?(level) }
81
- raise ArgumentError, "Invalid save_levels specified. Valid levels are: #{VALID_SAVE_LEVELS.join(', ')}"
82
- end
83
- end
84
-
85
- if @catch_exceptions
86
- at_exit do
87
- exception = $!
88
- _handle_exception(exception) if exception && exception.is_a?(Exception)
89
- end
90
- end
91
- end
92
-
93
- private
94
-
95
- def _handle_exception(exception)
96
- tb_str = exception.backtrace.join("\n")
97
- critical("Unhandled exception:\n#{exception.class}: #{exception.message}\n#{tb_str}")
98
- end
99
-
100
- def _worker
101
- loop do
102
- job = @log_queue.pop
103
- break if job.nil?
104
-
105
- case job[:type]
106
- when :log
107
- _save_log_sync(job[:log_text], job[:level])
108
- when :exception
109
- _handle_exception(job[:exception])
110
- end
111
- end
112
- end
113
-
114
- def _GetTime
115
- return Time.now.strftime("%Y-%m-%d %H:%M:%S")
116
- end
117
-
118
- def _GetCaller
119
- frame = caller(3, 1).first
120
- return nil if frame.nil?
121
-
122
- match = frame.match(/^(.+):(\d+):/)
123
- return nil unless match
124
-
125
- filename = File.basename(match[1])
126
- lineno = match[2]
127
- return "#{filename}:#{lineno}"
128
- end
129
-
130
- def _Cleanup_old_logs(days: 7)
131
- log_dir = File.join(Dir.pwd, 'log')
132
- return unless Dir.exist?(log_dir)
133
-
134
- now = Time.now
135
- Dir.entries(log_dir).each do |filename|
136
- next unless filename.end_with?('.log')
137
- next if filename == '.' || filename == '..'
138
-
139
- filepath = File.join(log_dir, filename)
140
-
141
- begin
142
- date_str = filename.gsub('.log', '')
143
- file_date = Date.strptime(date_str, '%Y-%m-%d').to_time
144
- rescue ArgumentError
145
- file_date = File.mtime(filepath)
146
- end
147
-
148
- if (now - file_date) / (24 * 60 * 60) >= days
149
- begin
150
- File.delete(filepath)
151
- info("[LOG CLEANUP] removed: #{filepath}") unless @silent
152
- rescue => e
153
- warning("[LOG CLEANUP WARNING] #{filepath} cannot be removed: #{e}") unless @silent
154
- end
155
- end
156
- end
157
- end
158
-
159
- def _save_log_sync(log_text, level)
160
- return unless @all_save
161
- return unless @save_levels.include?(level)
162
-
163
- log_dir = File.join(Dir.pwd, 'log')
164
- FileUtils.mkdir_p(log_dir)
165
- log_file = File.join(log_dir, Time.now.strftime('%Y-%m-%d') + '.log')
166
-
167
- File.open(log_file, 'a', encoding: 'utf-8') do |f|
168
- f.write(log_text + "\n")
169
- end
170
-
171
- today = Date.today
172
- if @last_cleanup_date != today
173
- _Cleanup_old_logs(days: 7)
174
- @last_cleanup_date = today
175
- end
176
- end
177
-
178
- def _save_log(log_text, level)
179
- return unless @save_levels.include?(level)
180
-
181
- log_dir = File.join(Dir.pwd, 'log')
182
- FileUtils.mkdir_p(log_dir) unless Dir.exist?(log_dir)
183
- log_file = File.join(log_dir, Time.now.strftime('%Y-%m-%d') + '.log')
184
-
185
- File.open(log_file, 'a', encoding: 'utf-8') do |f|
186
- f.write(log_text + "\n")
187
- end
188
- end
189
-
190
- def _Log(msg, tye, tag = nil)
191
- colors = COLOURS[tye.to_sym] || ""
192
- types = tye.upcase
193
-
194
- final_tag = tag || (@show_tag ? @tag : nil)
195
- tag_str = final_tag || ""
196
-
197
- caller_info = (@show_file || @enum == "file") ? _GetCaller() : ""
198
-
199
- if @notice && ["ERROR", "CRITICAL"].include?(types)
200
- Win32::Notification.new(
201
- title: "#{types} Log notice",
202
- message: msg,
203
- app_name: "VersaLog"
204
- ).show
205
- end
206
-
207
- case @enum
208
- when "simple"
209
- symbol = SYMBOLS[tye.to_sym] || "[?]"
210
- if @show_file
211
- formatted = "[#{caller_info}][#{tag_str}]#{colors}#{symbol}#{RESET} #{msg}"
212
- plain = "[#{caller_info}][#{tag_str}]#{symbol} #{msg}"
213
- else
214
- formatted = "#{colors}#{symbol}#{RESET} #{msg}"
215
- plain = "#{symbol} #{msg}"
216
- end
217
-
218
- when "simple2"
219
- symbol = SYMBOLS[tye.to_sym] || "[?]"
220
- time = _GetTime()
221
- if @show_file
222
- formatted = "[#{time}] [#{caller_info}][#{tag_str}]#{colors}#{symbol}#{RESET} #{msg}"
223
- plain = "[#{time}] [#{caller_info}][#{tag_str}]#{symbol} #{msg}"
224
- else
225
- formatted = "[#{time}] #{colors}#{symbol}#{RESET} #{msg}"
226
- plain = "[#{time}] #{symbol} #{msg}"
227
- end
228
-
229
- when "file"
230
- formatted = "[#{caller_info}]#{colors}[#{types}]#{RESET} #{msg}"
231
- plain = "[#{caller_info}][#{types}] #{msg}"
232
-
233
- else
234
- time = _GetTime()
235
- formatted = "[#{time}]#{colors}[#{types}]#{RESET}"
236
- plain = "[#{time}][#{types}]"
237
- if final_tag
238
- formatted += "[#{final_tag}]"
239
- plain += "[#{final_tag}]"
240
- end
241
- if @show_file
242
- formatted += "[#{caller_info}]"
243
- plain += "[#{caller_info}]"
244
- end
245
- formatted += " : #{msg}"
246
- plain += " : #{msg}"
247
- end
248
-
249
- puts(formatted) unless @silent
250
-
251
- @log_queue << { type: :log, log_text: plain, level: types }
252
- end
253
-
254
- def info(msg, tag = nil)
255
- _Log(msg, "INFO", tag)
256
- end
257
-
258
- def error(msg, tag = nil)
259
- _Log(msg, "ERROR", tag)
260
- end
261
-
262
- def warning(msg, tag = nil)
263
- _Log(msg, "WARNING", tag)
264
- end
265
-
266
- def debug(msg, tag = nil)
267
- _Log(msg, "DEBUG", tag)
268
- end
269
-
270
- def critical(msg, tag = nil)
271
- _Log(msg, "CRITICAL", tag)
272
- end
1
+ # frozen_string_literal: true
2
+
3
+ require "time"
4
+ require "thread"
5
+ require "date"
6
+ require "fileutils"
7
+
8
+ begin
9
+ require "win32/notification" if Gem.win_platform?
10
+ rescue LoadError
11
+ # win32-notification gem is not available
12
+ end
13
+
14
+ require_relative "versalog/version"
15
+
16
+ module Versalog
17
+ class VersaLog
18
+ COLOURS = {
19
+ INFO: "\033[32m",
20
+ ERROR: "\033[31m",
21
+ WARNING: "\033[33m",
22
+ DEBUG: "\033[36m",
23
+ CRITICAL: "\033[35m",
24
+ }.freeze
25
+
26
+ SYMBOLS = {
27
+ INFO: "[+]",
28
+ ERROR: "[-]",
29
+ WARNING: "[!]",
30
+ DEBUG: "[D]",
31
+ CRITICAL: "[C]",
32
+ }.freeze
33
+
34
+ RESET = "\033[0m"
35
+
36
+ VALID_MODES = ["simple", "simple2", "detailed", "file"].freeze
37
+ VALID_SAVE_LEVELS = ["INFO", "ERROR", "WARNING", "DEBUG", "CRITICAL"].freeze
38
+
39
+ def initialize(
40
+ enum: "simple",
41
+ tag: nil,
42
+ show_file: false,
43
+ show_tag: false,
44
+ enable_all: false,
45
+ notice: false,
46
+ all_save: false,
47
+ save_levels: nil,
48
+ silent: false,
49
+ catch_exceptions: false)
50
+
51
+ if enable_all
52
+ show_file = true
53
+ show_tag = true
54
+ notice = true
55
+ all_save = true
56
+ end
57
+
58
+ @enum = enum.downcase
59
+ @tag = tag
60
+ @show_file = show_file
61
+ @show_tag = show_tag
62
+ @enable_all = enable_all
63
+ @notice = notice
64
+ @all_save = all_save
65
+ @save_levels = save_levels
66
+ @silent = silent
67
+ @catch_exceptions = catch_exceptions
68
+
69
+ @log_queue = Queue.new
70
+ @worker_thread = Thread.new { _worker }
71
+ @last_cleanup_date = nil
72
+
73
+ unless VALID_MODES.include?(@enum)
74
+ raise ArgumentError, "Invalid enum: #{@enum}"
75
+ end
76
+
77
+ if @all_save
78
+ if @save_levels.nil?
79
+ @save_levels = VALID_SAVE_LEVELS.dup
80
+ elsif !@save_levels.is_a?(Array)
81
+ raise ArgumentError, "save_levels must be an Array. Example: ['ERROR']"
82
+ elsif !@save_levels.all? { |level| VALID_SAVE_LEVELS.include?(level) }
83
+ raise ArgumentError, "Invalid save_levels specified. Valid levels are: #{VALID_SAVE_LEVELS.join(', ')}"
84
+ end
85
+ end
86
+
87
+ if @catch_exceptions
88
+ at_exit do
89
+ exception = $!
90
+ _handle_exception(exception) if exception && exception.is_a?(Exception)
91
+ end
92
+ end
93
+ end
94
+
95
+ private
96
+
97
+ def _handle_exception(exception)
98
+ tb_str = exception.backtrace.join("\n")
99
+ critical("Unhandled exception:\n#{exception.class}: #{exception.message}\n#{tb_str}")
100
+ end
101
+
102
+ def _worker
103
+ loop do
104
+ job = @log_queue.pop
105
+ break if job.nil?
106
+
107
+ case job[:type]
108
+ when :log
109
+ _save_log_sync(job[:log_text], job[:level])
110
+ when :exception
111
+ _handle_exception(job[:exception])
112
+ end
113
+ end
114
+ end
115
+
116
+ def _GetTime
117
+ return Time.now.strftime("%Y-%m-%d %H:%M:%S")
118
+ end
119
+
120
+ def _GetCaller
121
+ frame = caller(3, 1).first
122
+ return nil if frame.nil?
123
+
124
+ match = frame.match(/^(.+):(\d+):/)
125
+ return nil unless match
126
+
127
+ filename = File.basename(match[1])
128
+ lineno = match[2]
129
+ return "#{filename}:#{lineno}"
130
+ end
131
+
132
+ def _Cleanup_old_logs(days: 7)
133
+ log_dir = File.join(Dir.pwd, 'log')
134
+ return unless Dir.exist?(log_dir)
135
+
136
+ now = Time.now
137
+ Dir.entries(log_dir).each do |filename|
138
+ next unless filename.end_with?('.log')
139
+ next if filename == '.' || filename == '..'
140
+
141
+ filepath = File.join(log_dir, filename)
142
+
143
+ begin
144
+ date_str = filename.gsub('.log', '')
145
+ file_date = Date.strptime(date_str, '%Y-%m-%d').to_time
146
+ rescue ArgumentError
147
+ file_date = File.mtime(filepath)
148
+ end
149
+
150
+ if (now - file_date) / (24 * 60 * 60) >= days
151
+ begin
152
+ File.delete(filepath)
153
+ info("[LOG CLEANUP] removed: #{filepath}") unless @silent
154
+ rescue => e
155
+ warning("[LOG CLEANUP WARNING] #{filepath} cannot be removed: #{e}") unless @silent
156
+ end
157
+ end
158
+ end
159
+ end
160
+
161
+ def _save_log_sync(log_text, level)
162
+ return unless @all_save
163
+ return unless @save_levels.include?(level)
164
+
165
+ log_dir = File.join(Dir.pwd, 'log')
166
+ FileUtils.mkdir_p(log_dir)
167
+ log_file = File.join(log_dir, Time.now.strftime('%Y-%m-%d') + '.log')
168
+
169
+ File.open(log_file, 'a', encoding: 'utf-8') do |f|
170
+ f.write(log_text + "\n")
171
+ end
172
+
173
+ today = Date.today
174
+ if @last_cleanup_date != today
175
+ _Cleanup_old_logs(days: 7)
176
+ @last_cleanup_date = today
177
+ end
178
+ end
179
+
180
+ def _save_log(log_text, level)
181
+ return unless @save_levels.include?(level)
182
+
183
+ log_dir = File.join(Dir.pwd, 'log')
184
+ FileUtils.mkdir_p(log_dir) unless Dir.exist?(log_dir)
185
+ log_file = File.join(log_dir, Time.now.strftime('%Y-%m-%d') + '.log')
186
+
187
+ File.open(log_file, 'a', encoding: 'utf-8') do |f|
188
+ f.write(log_text + "\n")
189
+ end
190
+ end
191
+
192
+ def _Log(msg, tye, tag = nil)
193
+ colors = COLOURS[tye.to_sym] || ""
194
+ types = tye.upcase
195
+
196
+ final_tag = tag || (@show_tag ? @tag : nil)
197
+ tag_str = final_tag || ""
198
+
199
+ caller_info = (@show_file || @enum == "file") ? _GetCaller() : ""
200
+
201
+ if @notice && ["ERROR", "CRITICAL"].include?(types)
202
+ begin
203
+ if defined?(Win32::Notification)
204
+ Win32::Notification.new(
205
+ title: "#{types} Log notice",
206
+ message: msg,
207
+ app_name: "VersaLog"
208
+ ).show
209
+ end
210
+ rescue => e
211
+ # Silently ignore notification errors
212
+ end
213
+ end
214
+
215
+ case @enum
216
+ when "simple"
217
+ symbol = SYMBOLS[tye.to_sym] || "[?]"
218
+ if @show_file
219
+ formatted = "[#{caller_info}][#{tag_str}]#{colors}#{symbol}#{RESET} #{msg}"
220
+ plain = "[#{caller_info}][#{tag_str}]#{symbol} #{msg}"
221
+ else
222
+ formatted = "#{colors}#{symbol}#{RESET} #{msg}"
223
+ plain = "#{symbol} #{msg}"
224
+ end
225
+
226
+ when "simple2"
227
+ symbol = SYMBOLS[tye.to_sym] || "[?]"
228
+ time = _GetTime()
229
+ if @show_file
230
+ formatted = "[#{time}] [#{caller_info}][#{tag_str}]#{colors}#{symbol}#{RESET} #{msg}"
231
+ plain = "[#{time}] [#{caller_info}][#{tag_str}]#{symbol} #{msg}"
232
+ else
233
+ formatted = "[#{time}] #{colors}#{symbol}#{RESET} #{msg}"
234
+ plain = "[#{time}] #{symbol} #{msg}"
235
+ end
236
+
237
+ when "file"
238
+ formatted = "[#{caller_info}]#{colors}[#{types}]#{RESET} #{msg}"
239
+ plain = "[#{caller_info}][#{types}] #{msg}"
240
+
241
+ else
242
+ time = _GetTime()
243
+ formatted = "[#{time}]#{colors}[#{types}]#{RESET}"
244
+ plain = "[#{time}][#{types}]"
245
+ if final_tag
246
+ formatted += "[#{final_tag}]"
247
+ plain += "[#{final_tag}]"
248
+ end
249
+ if @show_file
250
+ formatted += "[#{caller_info}]"
251
+ plain += "[#{caller_info}]"
252
+ end
253
+ formatted += " : #{msg}"
254
+ plain += " : #{msg}"
255
+ end
256
+
257
+ puts(formatted) unless @silent
258
+
259
+ @log_queue << { type: :log, log_text: plain, level: types }
260
+ end
261
+
262
+ public
263
+
264
+ def info(msg, tag = nil)
265
+ _Log(msg, "INFO", tag)
266
+ end
267
+
268
+ def error(msg, tag = nil)
269
+ _Log(msg, "ERROR", tag)
270
+ end
271
+
272
+ def warning(msg, tag = nil)
273
+ _Log(msg, "WARNING", tag)
274
+ end
275
+
276
+ def debug(msg, tag = nil)
277
+ _Log(msg, "DEBUG", tag)
278
+ end
279
+
280
+ def critical(msg, tag = nil)
281
+ _Log(msg, "CRITICAL", tag)
282
+ end
283
+
284
+ def step(title, step, total, tag = nil)
285
+ msg = "[STEP #{step}/#{total}] #{title}"
286
+ _Log(msg, "INFO", tag)
287
+ end
288
+
289
+ def progress(title, current, total, tag = nil)
290
+ percent = total.to_i > 0 ? ((current.to_f / total.to_f) * 100).to_i : 0
291
+ msg = "#{title} : #{percent}% (#{current}/#{total})"
292
+ _Log(msg, "INFO", tag)
293
+ end
294
+
295
+ def timer(title, tag = nil)
296
+ start_time = Time.now
297
+ _Log("#{title} : start", "INFO", tag)
298
+
299
+ begin
300
+ yield if block_given?
301
+ ensure
302
+ elapsed = Time.now - start_time
303
+ _Log("#{title} : done (#{format('%.2f', elapsed)}s)", "INFO", tag)
304
+ end
305
+ end
306
+
307
+ end
308
+ end
@@ -0,0 +1,72 @@
1
+ require "versalog"
2
+
3
+ # show_file False
4
+ logger = Versalog::VersaLog.new(
5
+ enum: "detailed"
6
+ )
7
+
8
+ logger.info("ok")
9
+ logger.error("err")
10
+ logger.warning("war")
11
+ logger.debug("deb")
12
+ logger.critical("cri")
13
+
14
+ # show_file True
15
+ logger = Versalog::VersaLog.new(
16
+ enum: "detailed",
17
+ show_file: true
18
+ )
19
+
20
+ logger.info("ok")
21
+ logger.error("err")
22
+ logger.warning("war")
23
+ logger.debug("deb")
24
+ logger.critical("cri")
25
+
26
+ # show_tag True
27
+ logger = Versalog::VersaLog.new(
28
+ enum: "detailed",
29
+ show_tag: true
30
+ )
31
+
32
+ logger.info("ok")
33
+ logger.error("err")
34
+ logger.warning("war")
35
+ logger.debug("deb")
36
+ logger.critical("cri")
37
+
38
+ # notice True
39
+ logger = Versalog::VersaLog.new(
40
+ enum: "detailed",
41
+ notice: true
42
+ )
43
+
44
+ logger.info("ok")
45
+ logger.error("err")
46
+ logger.warning("war")
47
+ logger.debug("deb")
48
+ logger.critical("cri")
49
+
50
+ # silent True
51
+ logger = Versalog::VersaLog.new(
52
+ enum: "detailed",
53
+ silent: true
54
+ )
55
+
56
+ logger.info("ok")
57
+ logger.error("err")
58
+ logger.warning("war")
59
+ logger.debug("deb")
60
+ logger.critical("cri")
61
+
62
+ # enable_all True
63
+ logger = Versalog::VersaLog.new(
64
+ enum: "detailed",
65
+ enable_all: true
66
+ )
67
+
68
+ logger.info("ok")
69
+ logger.error("err")
70
+ logger.warning("war")
71
+ logger.debug("deb")
72
+ logger.critical("cri")
data/sample/file.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "versalog"
2
+
3
+ logger = Versalog::VersaLog.new(
4
+ enum: "file",
5
+ show_file: true
6
+ )
7
+
8
+ logger.info("ok")
9
+ logger.error("err")
10
+ logger.warning("war")
11
+ logger.debug("deb")
12
+ logger.critical("cri")
@@ -0,0 +1,13 @@
1
+ require "versalog"
2
+
3
+ # show_file False
4
+ logger = Versalog::VersaLog.new(
5
+ enum: "detailed",
6
+ all_save: true
7
+ )
8
+
9
+ logger.info("ok")
10
+ logger.error("err")
11
+ logger.warning("war")
12
+ logger.debug("deb")
13
+ logger.critical("cri")
data/sample/simple.rb ADDED
@@ -0,0 +1,72 @@
1
+ require "versalog"
2
+
3
+ # show_file False
4
+ logger = Versalog::VersaLog.new(
5
+ enum: "simple"
6
+ )
7
+
8
+ logger.info("ok")
9
+ logger.error("err")
10
+ logger.warning("war")
11
+ logger.debug("deb")
12
+ logger.critical("cri")
13
+
14
+ # show_file True
15
+ logger = Versalog::VersaLog.new(
16
+ enum: "simple",
17
+ show_file: true
18
+ )
19
+
20
+ logger.info("ok")
21
+ logger.error("err")
22
+ logger.warning("war")
23
+ logger.debug("deb")
24
+ logger.critical("cri")
25
+
26
+ # show_tag True
27
+ logger = Versalog::VersaLog.new(
28
+ enum: "simple",
29
+ show_tag: true
30
+ )
31
+
32
+ logger.info("ok")
33
+ logger.error("err")
34
+ logger.warning("war")
35
+ logger.debug("deb")
36
+ logger.critical("cri")
37
+
38
+ # notice True
39
+ logger = Versalog::VersaLog.new(
40
+ enum: "simple",
41
+ notice: true
42
+ )
43
+
44
+ logger.info("ok")
45
+ logger.error("err")
46
+ logger.warning("war")
47
+ logger.debug("deb")
48
+ logger.critical("cri")
49
+
50
+ # silent True
51
+ logger = Versalog::VersaLog.new(
52
+ enum: "simple",
53
+ silent: true
54
+ )
55
+
56
+ logger.info("ok")
57
+ logger.error("err")
58
+ logger.warning("war")
59
+ logger.debug("deb")
60
+ logger.critical("cri")
61
+
62
+ # enable_all True
63
+ logger = Versalog::VersaLog.new(
64
+ enum: "simple",
65
+ enable_all: true
66
+ )
67
+
68
+ logger.info("ok")
69
+ logger.error("err")
70
+ logger.warning("war")
71
+ logger.debug("deb")
72
+ logger.critical("cri")
data/sample/simple2.rb ADDED
@@ -0,0 +1,72 @@
1
+ require "versalog"
2
+
3
+ # show_file False
4
+ logger = Versalog::VersaLog.new(
5
+ enum: "simple2"
6
+ )
7
+
8
+ logger.info("ok")
9
+ logger.error("err")
10
+ logger.warning("war")
11
+ logger.debug("deb")
12
+ logger.critical("cri")
13
+
14
+ # show_file True
15
+ logger = Versalog::VersaLog.new(
16
+ enum: "simple2",
17
+ show_file: true
18
+ )
19
+
20
+ logger.info("ok")
21
+ logger.error("err")
22
+ logger.warning("war")
23
+ logger.debug("deb")
24
+ logger.critical("cri")
25
+
26
+ # show_tag True
27
+ logger = Versalog::VersaLog.new(
28
+ enum: "simple2",
29
+ show_tag: true
30
+ )
31
+
32
+ logger.info("ok")
33
+ logger.error("err")
34
+ logger.warning("war")
35
+ logger.debug("deb")
36
+ logger.critical("cri")
37
+
38
+ # notice True
39
+ logger = Versalog::VersaLog.new(
40
+ enum: "simple2",
41
+ notice: true
42
+ )
43
+
44
+ logger.info("ok")
45
+ logger.error("err")
46
+ logger.warning("war")
47
+ logger.debug("deb")
48
+ logger.critical("cri")
49
+
50
+ # silent True
51
+ logger = Versalog::VersaLog.new(
52
+ enum: "simple2",
53
+ silent: true
54
+ )
55
+
56
+ logger.info("ok")
57
+ logger.error("err")
58
+ logger.warning("war")
59
+ logger.debug("deb")
60
+ logger.critical("cri")
61
+
62
+ # enable_all True
63
+ logger = Versalog::VersaLog.new(
64
+ enum: "simple2",
65
+ enable_all: true
66
+ )
67
+
68
+ logger.info("ok")
69
+ logger.error("err")
70
+ logger.warning("war")
71
+ logger.debug("deb")
72
+ logger.critical("cri")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: versalog
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaedeek
@@ -14,16 +14,18 @@ executables: []
14
14
  extensions: []
15
15
  extra_rdoc_files: []
16
16
  files:
17
- - ".rspec"
18
- - ".rubocop.yml"
19
- - CHANGELOG.md
20
- - CODE_OF_CONDUCT.md
21
17
  - LICENSE.txt
22
18
  - README.md
23
- - Rakefile
19
+ - examples/api_req.rb
20
+ - examples/progress_logging.rb
21
+ - image/logo.png
24
22
  - lib/versalog.rb
25
23
  - lib/versalog/version.rb
26
- - sig/versalog.rbs
24
+ - sample/detailed.rb
25
+ - sample/file.rb
26
+ - sample/log_save.rb
27
+ - sample/simple.rb
28
+ - sample/simple2.rb
27
29
  homepage: https://github.com/VersaLog/VersaLog.rb
28
30
  licenses:
29
31
  - MIT
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
data/.rubocop.yml DELETED
@@ -1,8 +0,0 @@
1
- AllCops:
2
- TargetRubyVersion: 3.1
3
-
4
- Style/StringLiterals:
5
- EnforcedStyle: double_quotes
6
-
7
- Style/StringLiteralsInInterpolation:
8
- EnforcedStyle: double_quotes
data/CHANGELOG.md DELETED
@@ -1,5 +0,0 @@
1
- ## [Unreleased]
2
-
3
- ## [0.1.0] - 2025-10-06
4
-
5
- - Initial release
data/CODE_OF_CONDUCT.md DELETED
@@ -1,132 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- We as members, contributors, and leaders pledge to make participation in our
6
- community a harassment-free experience for everyone, regardless of age, body
7
- size, visible or invisible disability, ethnicity, sex characteristics, gender
8
- identity and expression, level of experience, education, socio-economic status,
9
- nationality, personal appearance, race, caste, color, religion, or sexual
10
- identity and orientation.
11
-
12
- We pledge to act and interact in ways that contribute to an open, welcoming,
13
- diverse, inclusive, and healthy community.
14
-
15
- ## Our Standards
16
-
17
- Examples of behavior that contributes to a positive environment for our
18
- community include:
19
-
20
- * Demonstrating empathy and kindness toward other people
21
- * Being respectful of differing opinions, viewpoints, and experiences
22
- * Giving and gracefully accepting constructive feedback
23
- * Accepting responsibility and apologizing to those affected by our mistakes,
24
- and learning from the experience
25
- * Focusing on what is best not just for us as individuals, but for the overall
26
- community
27
-
28
- Examples of unacceptable behavior include:
29
-
30
- * The use of sexualized language or imagery, and sexual attention or advances of
31
- any kind
32
- * Trolling, insulting or derogatory comments, and personal or political attacks
33
- * Public or private harassment
34
- * Publishing others' private information, such as a physical or email address,
35
- without their explicit permission
36
- * Other conduct which could reasonably be considered inappropriate in a
37
- professional setting
38
-
39
- ## Enforcement Responsibilities
40
-
41
- Community leaders are responsible for clarifying and enforcing our standards of
42
- acceptable behavior and will take appropriate and fair corrective action in
43
- response to any behavior that they deem inappropriate, threatening, offensive,
44
- or harmful.
45
-
46
- Community leaders have the right and responsibility to remove, edit, or reject
47
- comments, commits, code, wiki edits, issues, and other contributions that are
48
- not aligned to this Code of Conduct, and will communicate reasons for moderation
49
- decisions when appropriate.
50
-
51
- ## Scope
52
-
53
- This Code of Conduct applies within all community spaces, and also applies when
54
- an individual is officially representing the community in public spaces.
55
- Examples of representing our community include using an official email address,
56
- posting via an official social media account, or acting as an appointed
57
- representative at an online or offline event.
58
-
59
- ## Enforcement
60
-
61
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
- reported to the community leaders responsible for enforcement at
63
- [INSERT CONTACT METHOD].
64
- All complaints will be reviewed and investigated promptly and fairly.
65
-
66
- All community leaders are obligated to respect the privacy and security of the
67
- reporter of any incident.
68
-
69
- ## Enforcement Guidelines
70
-
71
- Community leaders will follow these Community Impact Guidelines in determining
72
- the consequences for any action they deem in violation of this Code of Conduct:
73
-
74
- ### 1. Correction
75
-
76
- **Community Impact**: Use of inappropriate language or other behavior deemed
77
- unprofessional or unwelcome in the community.
78
-
79
- **Consequence**: A private, written warning from community leaders, providing
80
- clarity around the nature of the violation and an explanation of why the
81
- behavior was inappropriate. A public apology may be requested.
82
-
83
- ### 2. Warning
84
-
85
- **Community Impact**: A violation through a single incident or series of
86
- actions.
87
-
88
- **Consequence**: A warning with consequences for continued behavior. No
89
- interaction with the people involved, including unsolicited interaction with
90
- those enforcing the Code of Conduct, for a specified period of time. This
91
- includes avoiding interactions in community spaces as well as external channels
92
- like social media. Violating these terms may lead to a temporary or permanent
93
- ban.
94
-
95
- ### 3. Temporary Ban
96
-
97
- **Community Impact**: A serious violation of community standards, including
98
- sustained inappropriate behavior.
99
-
100
- **Consequence**: A temporary ban from any sort of interaction or public
101
- communication with the community for a specified period of time. No public or
102
- private interaction with the people involved, including unsolicited interaction
103
- with those enforcing the Code of Conduct, is allowed during this period.
104
- Violating these terms may lead to a permanent ban.
105
-
106
- ### 4. Permanent Ban
107
-
108
- **Community Impact**: Demonstrating a pattern of violation of community
109
- standards, including sustained inappropriate behavior, harassment of an
110
- individual, or aggression toward or disparagement of classes of individuals.
111
-
112
- **Consequence**: A permanent ban from any sort of public interaction within the
113
- community.
114
-
115
- ## Attribution
116
-
117
- This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
- version 2.1, available at
119
- [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
-
121
- Community Impact Guidelines were inspired by
122
- [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
-
124
- For answers to common questions about this code of conduct, see the FAQ at
125
- [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
- [https://www.contributor-covenant.org/translations][translations].
127
-
128
- [homepage]: https://www.contributor-covenant.org
129
- [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
- [Mozilla CoC]: https://github.com/mozilla/diversity
131
- [FAQ]: https://www.contributor-covenant.org/faq
132
- [translations]: https://www.contributor-covenant.org/translations
data/Rakefile DELETED
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
5
-
6
- RSpec::Core::RakeTask.new(:spec)
7
-
8
- require "rubocop/rake_task"
9
-
10
- RuboCop::RakeTask.new
11
-
12
- task default: %i[spec rubocop]
data/sig/versalog.rbs DELETED
@@ -1,4 +0,0 @@
1
- module Versalog
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end