itz_logger 0.2.10 → 0.3.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: 752da29c81d20b8e1126b8c6d217cba564c2f54651be5d028fb2e0b7ac74d7d3
4
- data.tar.gz: 6aabe0d9fe4051fb07a166af621e19792123494f9103889e721cf125f4ace368
3
+ metadata.gz: 2814d878b3037ce6e88598e31ad83aea5715f1f39d0432d80c40d90bf33a566d
4
+ data.tar.gz: b7e119a2dc786bfa20248bbf2cce3c6bd62415b699d910c76f114fb73b6672ee
5
5
  SHA512:
6
- metadata.gz: 223b9715ba89415fcd70ffa7032e4f9889e62750071dcf1cc049dbc7b1747e99f03107f1e76c725a3177f51d9fad044998816a1e6ffe88c4a578f2b3ac3278e4
7
- data.tar.gz: e2c2a5873f9ab04014bc4c6a8246c2576bf5bc340a0173f8dacc0a1b5d850189153a570c6a659ca05c4154090cf4897ec8d0150f1fd889a2f8fb367c71ce6f04
6
+ metadata.gz: 4a03ddc86fa7806148a9f4de1b8689cb0aed9d1a755e1e53f87caa68c09fe7a4a76cdc4a2fc1ed8c9e65eb9a1ae88ff8a228efac6db33d2479a388c836eede5d
7
+ data.tar.gz: 06027edb5392e3a31b8af7d67312471eadc441c008eb8918edf437ffed89c84a88bc0197a5de68fdc46a165c68690a5a1ffc31e290a82d250420b1c120de5e3f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- itz_logger (0.2.9)
4
+ itz_logger (0.2.10)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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 (not yet implemented)
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(log_level, log_target)
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.
@@ -9,7 +9,7 @@ module ItzLogger
9
9
  class Formatter
10
10
 
11
11
  def self.format(id, message)
12
- printf(
12
+ sprintf(
13
13
  "%-19s %-s8 %-#{message.type.length}s %s\n",
14
14
  message.created_at.strftime("%b %d %H:%M:%S UTC"),
15
15
  id,
@@ -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 = "/tmp/logfile.log")
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(_log_level, _message)
20
- puts "not yet implemented"
21
- # return nil if !ItzLogger::MessageType.equals(@log_level, log_level)
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
- # message = ItzLogger::Message.new(message, log_level)
24
- # ItzLogger::Formatter.format(message)
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
@@ -4,6 +4,7 @@ module ItzLogger
4
4
  class LogStrategy
5
5
 
6
6
  LOG_TERMINAL = :log_terminal
7
+ LOG_FILE = :log_file
7
8
  LOG_REDIS_MAIL = :log_redis_mail
8
9
  LOG_RABBITMQ = :log_rabbitmq
9
10
 
@@ -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
- message = ItzLogger::Message.new(message, log_level)
22
- ItzLogger::Formatter.format(@log_id, message)
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)
@@ -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
- @id = nil
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
- _log_file = options.fetch(:log_file, "./log/logfile.log")
19
- @id = SecureRandom.hex(4)
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, @id)
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
- @id
60
+ @log_id
60
61
  end
61
62
 
62
63
  end
@@ -1,3 +1,3 @@
1
1
  module ItzLogger
2
- VERSION = "0.2.10"
2
+ VERSION = "0.3.0"
3
3
  end
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.2.10
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-23 00:00:00.000000000 Z
11
+ date: 2018-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler