itz_logger 0.2.10 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +48 -2
- data/lib/itz_logger/formatter.rb +1 -1
- data/lib/itz_logger/log_file.rb +91 -6
- data/lib/itz_logger/log_strategy.rb +1 -0
- data/lib/itz_logger/log_terminal.rb +2 -2
- data/lib/itz_logger/logger.rb +7 -6
- data/lib/itz_logger/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2814d878b3037ce6e88598e31ad83aea5715f1f39d0432d80c40d90bf33a566d
|
4
|
+
data.tar.gz: b7e119a2dc786bfa20248bbf2cce3c6bd62415b699d910c76f114fb73b6672ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a03ddc86fa7806148a9f4de1b8689cb0aed9d1a755e1e53f87caa68c09fe7a4a76cdc4a2fc1ed8c9e65eb9a1ae88ff8a228efac6db33d2479a388c836eede5d
|
7
|
+
data.tar.gz: 06027edb5392e3a31b8af7d67312471eadc441c008eb8918edf437ffed89c84a88bc0197a5de68fdc46a165c68690a5a1ffc31e290a82d250420b1c120de5e3f
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -5,7 +5,7 @@ ItzLogger is the N+1 log gem. Following the rule, that at least once in a life t
|
|
5
5
|
This log gem supports multiple output channels:
|
6
6
|
|
7
7
|
1. log to the terminal (stdout only)
|
8
|
-
2. log to a file
|
8
|
+
2. log to a file
|
9
9
|
3. log to a RabbitMQ service (not yet implemented)
|
10
10
|
|
11
11
|
|
@@ -52,7 +52,9 @@ log_level = :info
|
|
52
52
|
log_target = :log_terminal
|
53
53
|
|
54
54
|
# create a logger
|
55
|
-
logger = ItzLogger::Logger.new(
|
55
|
+
logger = ItzLogger::Logger.new(
|
56
|
+
log_level: log_level,
|
57
|
+
log_strategy: log_target)
|
56
58
|
|
57
59
|
# now go ahead
|
58
60
|
logger.info("foo")
|
@@ -64,6 +66,50 @@ logger.debug("baz")
|
|
64
66
|
logger.verbose("foo bar baz")
|
65
67
|
```
|
66
68
|
|
69
|
+
### Log to a file
|
70
|
+
|
71
|
+
First things first - install the gem. Take a look in the installation section.
|
72
|
+
|
73
|
+
The logger checks the existance of the log file. If the file exists and
|
74
|
+
is writable, the logger uses this the file.
|
75
|
+
If the file does not exist, the logger tries to create the file.
|
76
|
+
|
77
|
+
If both fails (creation and/or usage), the logger silently ignores it and does
|
78
|
+
write any logs.
|
79
|
+
|
80
|
+
A simple example:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
require 'itz_logger'
|
84
|
+
|
85
|
+
...
|
86
|
+
|
87
|
+
# you can choose the log_level from from
|
88
|
+
# - :info
|
89
|
+
# - :warn
|
90
|
+
# - :debug
|
91
|
+
# - :verbose
|
92
|
+
log_level = :info
|
93
|
+
|
94
|
+
# you can choose the target from
|
95
|
+
# - :log_terminal
|
96
|
+
log_target = :log_file
|
97
|
+
|
98
|
+
# create a logger
|
99
|
+
logger = ItzLogger::Logger.new(
|
100
|
+
log_level: log_level,
|
101
|
+
log_strategy: log_target,
|
102
|
+
log_file: "/tmp/my_log_file.log")
|
103
|
+
|
104
|
+
# now go ahead
|
105
|
+
logger.info("foo")
|
106
|
+
|
107
|
+
logger.warn("bar")
|
108
|
+
|
109
|
+
logger.debug("baz")
|
110
|
+
|
111
|
+
logger.verbose("foo bar baz")
|
112
|
+
```
|
67
113
|
## Development
|
68
114
|
|
69
115
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/itz_logger/formatter.rb
CHANGED
data/lib/itz_logger/log_file.rb
CHANGED
@@ -1,4 +1,8 @@
|
|
1
1
|
|
2
|
+
# Log file writer
|
3
|
+
#
|
4
|
+
# This class encapsulate the file writer of the itz_logger project.
|
5
|
+
|
2
6
|
require_relative "./message"
|
3
7
|
require_relative "./formatter"
|
4
8
|
|
@@ -6,22 +10,42 @@ module ItzLogger
|
|
6
10
|
class LogFile
|
7
11
|
|
8
12
|
@log_level = nil
|
13
|
+
@log_id = nil
|
14
|
+
@log_file_handle = nil
|
15
|
+
|
16
|
+
@error_flag = false
|
17
|
+
@error_reason = nil
|
9
18
|
|
10
19
|
attr_reader :log_level
|
11
20
|
|
12
21
|
def initialize(
|
13
22
|
log_level = ItzLogger::MessageType::INFO,
|
14
|
-
log_file =
|
23
|
+
log_file = '/tmp/logfile.log',
|
24
|
+
log_id = nil)
|
15
25
|
|
16
26
|
@log_level = log_level
|
27
|
+
@log_id = log_id
|
28
|
+
|
29
|
+
create_and_open_log_file(log_file)
|
30
|
+
check_error
|
17
31
|
end
|
18
32
|
|
19
|
-
def write(
|
20
|
-
|
21
|
-
#
|
33
|
+
def write(log_level, message)
|
34
|
+
|
35
|
+
# stop here, iff there is no file to write into
|
36
|
+
return nil if check_error
|
37
|
+
|
38
|
+
# stop here, iff the message is not importand enough
|
39
|
+
return nil if !ItzLogger::MessageType.equals(@log_level, log_level)
|
40
|
+
|
41
|
+
log_message = ItzLogger::Message.new(message, log_level)
|
22
42
|
|
23
|
-
|
24
|
-
|
43
|
+
File.write(
|
44
|
+
@log_file_handle,
|
45
|
+
ItzLogger::Formatter.format(
|
46
|
+
@log_id,
|
47
|
+
log_message),
|
48
|
+
mode: 'a')
|
25
49
|
end
|
26
50
|
|
27
51
|
def log_level=(log_level)
|
@@ -29,5 +53,66 @@ module ItzLogger
|
|
29
53
|
@log_level = log_level
|
30
54
|
end
|
31
55
|
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def check_error
|
60
|
+
if @error_reason == true
|
61
|
+
STDERR.puts @error_reason
|
62
|
+
end
|
63
|
+
|
64
|
+
@error_flag
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
# create or/and open the log file
|
69
|
+
def create_and_open_log_file(log_file)
|
70
|
+
# check existance of the given file
|
71
|
+
# and do we have permission to write
|
72
|
+
if File.exist?(log_file)
|
73
|
+
if File.writable_real?(log_file)
|
74
|
+
@log_file_handle = File.open(log_file, 'a')
|
75
|
+
@log_file_handle.sync = true
|
76
|
+
else
|
77
|
+
@error_flag = true
|
78
|
+
@error_reason = "[ERROR] can't open log file for writing!"
|
79
|
+
end
|
80
|
+
else
|
81
|
+
path = File.dirname(log_file)
|
82
|
+
_filename = File.basename(log_file)
|
83
|
+
|
84
|
+
# check whether the path to the log file exists and is writable
|
85
|
+
#
|
86
|
+
# if not, try to create the path
|
87
|
+
if Dir.exist?(path)
|
88
|
+
|
89
|
+
if File.writable_real?(path)
|
90
|
+
begin
|
91
|
+
@log_file_handle = File.open(log_file, 'w')
|
92
|
+
@log_file_handle.sync = true
|
93
|
+
rescue
|
94
|
+
@error_flag = true
|
95
|
+
@error_reason = "[error] can't create log file"
|
96
|
+
end
|
97
|
+
|
98
|
+
else
|
99
|
+
@error_flag = true
|
100
|
+
@error_reason = "[error] can't create log file #{log_file}"
|
101
|
+
end
|
102
|
+
|
103
|
+
else
|
104
|
+
|
105
|
+
begin
|
106
|
+
FileUtils.mkdir_p(path)
|
107
|
+
@log_file_handle = File.open(log_file)
|
108
|
+
@log_file_handle.sync = true
|
109
|
+
rescue
|
110
|
+
@error_flag = true
|
111
|
+
@error_reason = "[error] can't create log file #{log_file}"
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
32
117
|
end
|
33
118
|
end
|
@@ -18,8 +18,8 @@ module ItzLogger
|
|
18
18
|
|
19
19
|
def write(log_level, message)
|
20
20
|
return nil if !ItzLogger::MessageType.equals(@log_level, log_level)
|
21
|
-
|
22
|
-
ItzLogger::Formatter.format(@log_id,
|
21
|
+
log_message = ItzLogger::Message.new(message, log_level)
|
22
|
+
puts ItzLogger::Formatter.format(@log_id, log_message)
|
23
23
|
end
|
24
24
|
|
25
25
|
def log_level=(log_level)
|
data/lib/itz_logger/logger.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
|
2
2
|
require_relative "./log_terminal"
|
3
|
+
require_relative "./log_file"
|
3
4
|
require_relative "./log_strategy"
|
4
5
|
|
5
6
|
require 'securerandom'
|
@@ -9,21 +10,21 @@ module ItzLogger
|
|
9
10
|
class Logger
|
10
11
|
|
11
12
|
@log_strategy = nil
|
12
|
-
@
|
13
|
+
@log_id = nil
|
13
14
|
|
14
15
|
def initialize(options = {})
|
15
16
|
|
16
17
|
log_level = options.fetch(:log_level, ItzLogger::MessageType::INFO)
|
17
18
|
log_strategy = options.fetch(:log_strategy, ItzLogger::LogStrategy::LOG_TERMINAL)
|
18
|
-
|
19
|
-
@
|
19
|
+
log_file = options.fetch(:log_file, "./log/logfile.log")
|
20
|
+
@log_id = SecureRandom.hex(4)
|
20
21
|
|
21
22
|
@log_strategy =
|
22
23
|
case log_strategy
|
23
24
|
when ItzLogger::LogStrategy::LOG_TERMINAL
|
24
|
-
ItzLogger::LogTerminal.new(log_level, @
|
25
|
+
ItzLogger::LogTerminal.new(log_level, @log_id)
|
25
26
|
when ItzLogger::LogStrategy::LOG_FILE
|
26
|
-
ItzLogger::LogFile.new(log_level)
|
27
|
+
ItzLogger::LogFile.new(log_level, log_file, @log_id)
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
@@ -56,7 +57,7 @@ module ItzLogger
|
|
56
57
|
end
|
57
58
|
|
58
59
|
def id
|
59
|
-
@
|
60
|
+
@log_id
|
60
61
|
end
|
61
62
|
|
62
63
|
end
|
data/lib/itz_logger/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: itz_logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Schaarschmidt
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-08-
|
11
|
+
date: 2018-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|