log_redux 1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/log_redux/logger.rb +126 -0
- data/lib/log_redux/logger_helpers.rb +15 -0
- data/lib/log_redux.rb +3 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1168023cc9b033774087cf359edc109875b643bdee832eb7b80c23acb5c85fd9
|
4
|
+
data.tar.gz: 6b6af03c951be721b0dd7a879bb2e3acf4fae3b4f6f6ca385ee0133aca777b04
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4ef5ead8bd4bf488748bef0ca1eef73f917b22c3a90b1c162443bb6d317993cebe454a74114f15d1a789a1bd3401d18893fb63db26b055640fe93441cbc03a8f
|
7
|
+
data.tar.gz: 8390a6092cd6aac4299e1c9ac99ab0dbb58180719d72b0e7348f932cb1222f75ba5ddf5fa22966ea71c34eb8fcee3a0f2f21d2fb3611fdf534ae83873a53f294
|
@@ -0,0 +1,126 @@
|
|
1
|
+
require_relative 'logger_helpers'
|
2
|
+
|
3
|
+
module LogRedux
|
4
|
+
class Logger
|
5
|
+
attr_reader :output_filename, :log_file, :history
|
6
|
+
attr_accessor :color, :timestamp, :filename, :line_number, :track
|
7
|
+
|
8
|
+
def initialize(output_filename = $stderr, color: true, timestamp: true, filename: true, track: false)
|
9
|
+
if output_filename.nil? || (!output_filename.is_a?(String) && !check_valid_stdio(output_filename))
|
10
|
+
raise LoggerError, 'invalid log file'
|
11
|
+
end
|
12
|
+
|
13
|
+
@output_filename = output_filename
|
14
|
+
@color = color
|
15
|
+
@timestamp = timestamp
|
16
|
+
@filename = filename
|
17
|
+
@track = track
|
18
|
+
@history = []
|
19
|
+
|
20
|
+
@log_file = check_valid_stdio(output_filename) ? output_filename : File.open(output_filename, 'a+')
|
21
|
+
end
|
22
|
+
|
23
|
+
def log(level, msg: nil)
|
24
|
+
assert_log_level(level)
|
25
|
+
|
26
|
+
level_str = level.to_s
|
27
|
+
time = Time.now.strftime('%H:%M:%S')
|
28
|
+
file_line = caller(1).last.split(':')[0..1]
|
29
|
+
filename = file_line[0]
|
30
|
+
line_number = file_line[1]
|
31
|
+
|
32
|
+
output = if @color
|
33
|
+
[
|
34
|
+
@timestamp ? "#{COLORS[:GREY]}#{time}" : nil,
|
35
|
+
"#{COLORS[level.to_sym]}#{level_str}#{@filename ? '' : ':'}",
|
36
|
+
@filename ? "#{COLORS[:GREY]}#{file_line.join(':')}:" : nil,
|
37
|
+
"#{COLORS[:WHITE]}#{msg}#{COLORS[:RESET]}"
|
38
|
+
].compact.join(' ')
|
39
|
+
else
|
40
|
+
[
|
41
|
+
@timestamp ? "#{time}" : nil,
|
42
|
+
"#{level_str}#{@filename ? '' : ':'}",
|
43
|
+
@filename ? "#{file_line.join(':')}:" : nil,
|
44
|
+
"#{msg}"
|
45
|
+
].compact.join(' ')
|
46
|
+
end
|
47
|
+
|
48
|
+
@log_file.puts output
|
49
|
+
|
50
|
+
save_entry(level, msg, time, filename, line_number, output) if @track
|
51
|
+
|
52
|
+
output
|
53
|
+
end
|
54
|
+
|
55
|
+
def trace(msg)
|
56
|
+
log(:TRACE, msg: msg)
|
57
|
+
end
|
58
|
+
|
59
|
+
def debug(msg)
|
60
|
+
log(:DEBUG, msg: msg)
|
61
|
+
end
|
62
|
+
|
63
|
+
def info(msg)
|
64
|
+
log(:INFO, msg: msg)
|
65
|
+
end
|
66
|
+
|
67
|
+
def warn(msg)
|
68
|
+
log(:WARN, msg: msg)
|
69
|
+
end
|
70
|
+
|
71
|
+
def error(msg)
|
72
|
+
log(:ERROR, msg: msg)
|
73
|
+
end
|
74
|
+
|
75
|
+
def fatal(msg)
|
76
|
+
log(:FATAL, msg: msg)
|
77
|
+
end
|
78
|
+
|
79
|
+
def [](index)
|
80
|
+
raise LoggerError, 'tracking logs is disabled' unless @track
|
81
|
+
|
82
|
+
@history[index][:formatted]
|
83
|
+
end
|
84
|
+
|
85
|
+
def first
|
86
|
+
raise LoggerError, 'tracking logs is disabled' unless @track
|
87
|
+
|
88
|
+
@history.first[:formatted]
|
89
|
+
end
|
90
|
+
|
91
|
+
def last
|
92
|
+
raise LoggerError, 'tracking logs is disabled' unless @track
|
93
|
+
|
94
|
+
@history.last[:formatted]
|
95
|
+
end
|
96
|
+
|
97
|
+
def close
|
98
|
+
raise LoggerError, 'cannot close a Standard IO file' if check_valid_stdio(@log_file)
|
99
|
+
|
100
|
+
@log_file.close
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
|
105
|
+
def check_valid_stdio(file)
|
106
|
+
return true if [$stdout, $stderr].include?(file)
|
107
|
+
|
108
|
+
false
|
109
|
+
end
|
110
|
+
|
111
|
+
def assert_log_level(level)
|
112
|
+
raise LoggerError, "invalid log level `#{level}`" unless COLORS.keys.include?(level.to_sym)
|
113
|
+
end
|
114
|
+
|
115
|
+
def save_entry(level, msg, time, filename, line_number, output)
|
116
|
+
@history << {
|
117
|
+
level: level,
|
118
|
+
time: time,
|
119
|
+
filename: filename,
|
120
|
+
line: line_number,
|
121
|
+
msg: msg,
|
122
|
+
formatted: output
|
123
|
+
}
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module LogRedux
|
2
|
+
COLORS = {
|
3
|
+
TRACE: "\e[94m",
|
4
|
+
DEBUG: "\e[36m",
|
5
|
+
INFO: "\e[32m",
|
6
|
+
WARN: "\e[33m",
|
7
|
+
ERROR: "\e[31m",
|
8
|
+
FATAL: "\e[35m",
|
9
|
+
GREY: "\e[90m",
|
10
|
+
WHITE: "\e[37m",
|
11
|
+
RESET: "\e[0m"
|
12
|
+
}
|
13
|
+
|
14
|
+
class LoggerError < StandardError; end
|
15
|
+
end
|
data/lib/log_redux.rb
ADDED
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: log_redux
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ualace Henrique
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-10-28 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple but rather customizable logging library for Ruby
|
14
|
+
email: ualacehenrique@hotmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/log_redux.rb
|
20
|
+
- lib/log_redux/logger.rb
|
21
|
+
- lib/log_redux/logger_helpers.rb
|
22
|
+
homepage: https://github.com/UalaceCafe/log_redux
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubygems_version: 3.4.19
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: LogRedux
|
45
|
+
test_files: []
|