logger_json 1.0.5 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5bf667f25b2c54910ab3e5699df13fa6fb73ae0aa5938971c55accf84534bb37
4
- data.tar.gz: 6f40a187db5aea92b1ad3b05ec0f7e43db7adf4aced004e544da7edb05adf095
3
+ metadata.gz: e65dc39558895b55dc0680b2ce613730a9e6e95cfb19fc84dc62e680c1c637d4
4
+ data.tar.gz: eecf814bb917975a3508ff9df08a34946a8309e1a76f09a000f2b48684291394
5
5
  SHA512:
6
- metadata.gz: 01a57b5dac44cac2c73a3a7c6aa80fa15433b8e80c41ae8add3e748cb37128d19908e54dc6030bf3349dd4f84d962d1f7fde3120d907d2e1573b11a7f15b159b
7
- data.tar.gz: 98dfbbd52f7cd6f5f267f1bd88d55ea602baddbc3de31a98533bda21bc539086d6f3fed563673ffbfec34ea56ddbbdf76e62801bee5f80e0eda61e2de825d24e
6
+ metadata.gz: e8fe54cbac887f552fc4e364e1fa343677a664fad7ef8da9298a74550873c3987cb4f16a8ca1bb959562f6bca4f0236dbc6554471d4b2cdbca51ddf085bd19cc
7
+ data.tar.gz: 89776b2814cb9756097de973f6e22470b63d0e339d1b3bd4e72dc39cc8a03292339b1252ce30fd9e7d6e583a8384605a74ae58717a7e7ebc18f5d989093dbe74
data/CHANGELOG.md CHANGED
@@ -0,0 +1,11 @@
1
+ ### 1.0.4 - 2020-02-06
2
+
3
+ - bug fixes
4
+ - Fixed the host attribute in model json
5
+ - Fixed the system not found log folder error
6
+
7
+ ### 1.1.0 - 2020-02-07
8
+
9
+ - enhancements
10
+ - Support Syslog/logger
11
+ - changed the display of a json parameter in hash format
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- **LoggerJson** is a gem written in Ruby that adds new functionality, without changing the function of the [Logger](https://ruby-doc.org/stdlib-2.4.0/libdoc/logger/rdoc/Logger.html) class methods, which creates the logs in the json format.
1
+ **LoggerJson** is a gem written in Ruby that adds new functionality, without changing the function of the [Logger](https://ruby-doc.org/stdlib-2.4.0/libdoc/logger/rdoc/Logger.html) or [Syslog::Logger](https://docs.ruby-lang.org/en/2.1.0/Syslog/Logger.html) classes methods, which creates the logs in the json format.
2
2
 
3
3
  **Getting started**
4
4
 
@@ -11,13 +11,7 @@ gem install logger_json
11
11
  Or add the following line to your `Gemfile`:
12
12
 
13
13
  ```ruby
14
- gem 'logger_json', '~> 1.0', '>= 1.0.4'
15
- ```
16
-
17
- Before `version 1.0.4` you have to create a logs folder, if you don't have one:
18
-
19
- ```ruby
20
- mkdir logs
14
+ gem 'logger_json'
21
15
  ```
22
16
 
23
17
  Then run `bundle install`
@@ -28,9 +22,10 @@ Just initialize the gem and watch the magic happen
28
22
  require 'logger_json'
29
23
  ```
30
24
 
31
- If you don't create a Logger with specified file, don't be discouraged, you can set a default output for json logs
25
+ If you don't create a Logger with specified file, don't be discouraged, you can set a environment var or ruby path var
32
26
 
33
27
  ```ruby
28
+ ENV['LOG_PATH'] #or
34
29
  LoggerJson::Text::PATH = 'path_you_desire'
35
30
  ```
36
31
 
@@ -39,9 +34,9 @@ LoggerJson::Text::PATH = 'path_you_desire'
39
34
  ```ruby
40
35
  {
41
36
  date: "2020-02-05T13:46:11.026134758+00:00", #iso8601
42
- host: "https://www.r7.com/",
37
+ host: "172.0.0.10",
43
38
  thread: 'current_thread',
44
- methods: ['create_json'],
39
+ methods: ['create_json', 'run', ... , ''],
45
40
  level: 'info',
46
41
  message: 'LoggerJson is a gem written in Ruby that ...'
47
42
  }
@@ -49,17 +44,30 @@ LoggerJson::Text::PATH = 'path_you_desire'
49
44
 
50
45
  **To add parameters in the json logger**
51
46
 
47
+ 1. Logger class
48
+
52
49
  ```ruby
53
50
  more_params = {key1: data, key2: data ... keyN: data}
54
51
  Logger.new('output').warn(message, more_params)
55
52
  ```
56
53
 
54
+ 2. Syslog::Logger class
55
+
56
+ ```ruby
57
+ LoggerJson::Text::PATH = './logs/logs-json.log'
58
+ logger = Syslog::Logger.new 'logger_json'
59
+ more_params = {key1: data, key2: data ... keyN: data}
60
+ logger.info(message, more_params)
61
+ ```
62
+
57
63
  **Compatibility**
58
64
 
59
65
  - Ruby 1.9+
60
66
 
61
67
  **Running tests**
62
68
 
69
+ Download the source code
70
+
63
71
  LoggerJson uses [Mini Test](https://github.com/seattlerb/minitest) as test framework.
64
72
 
65
73
  * Ruby version > 2.0
@@ -0,0 +1,30 @@
1
+ module LoggerJsonCore
2
+ def self.included(klass)
3
+ %w{debug info warn error fatal}.each do |level|
4
+ klass.send(:alias_method, "old_#{level}", level)
5
+
6
+ block = Proc.new do |*args, &block|
7
+ create_file args, level
8
+ return send("old_#{level}", args[0]) { block.call } unless block.nil?
9
+ send "old_#{level}", args[0]
10
+ end
11
+
12
+ klass.send(:define_method, level, block)
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def create_file(message, type=:info)
19
+ path = filename || ENV['LOG_PATH'] || LoggerJson::Text::PATH
20
+ json = LoggerJson::Model.new(message, type).json
21
+ LoggerJson::Writter.logger_json(path, json)
22
+ end
23
+
24
+ def filename
25
+ return unless defined?(Syslog::Logger).nil?
26
+ path = instance_variable_get('@logdev').filename
27
+ return if path.nil?
28
+ path.gsub('.log','-json.log')
29
+ end
30
+ end
data/lib/logger_json.rb CHANGED
@@ -1,30 +1,34 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- if defined?(JSON).nil?
3
+ unless defined?(JSON)
4
4
  require 'json'
5
5
  end
6
6
 
7
- if defined?(DateTime).nil?
7
+ unless defined?(DateTime)
8
8
  require 'date'
9
9
  end
10
10
 
11
+ unless defined?(Socket)
12
+ require 'socket'
13
+ end
14
+
15
+ require 'logger_json/text'
16
+ require 'logger_json/writter'
17
+ require 'logger_json/model'
18
+
11
19
  def load_logger_json
12
- if defined?(Logger).nil?
20
+ unless defined?(Logger)
13
21
  require 'logger'
14
22
  end
15
-
16
- require 'logger_json/text'
23
+ require 'core/logger_json_core'
17
24
  Logger.new(STDOUT).info(LoggerJson::Text::LOGGER_JSON)
18
-
19
25
  require 'logger_json/logger'
20
- require 'logger_json/writter'
21
- require 'logger_json/json/model'
22
- require 'logger_json/json/logger'
23
26
  end
24
27
 
25
28
  def load_syslog_json
26
- require 'syslog_json/text'
27
- Logger.new(STDOUT).info(Syslog::Text::LOGGER_JSON)
29
+ require 'core/logger_json_core'
30
+ Logger.new(STDOUT).info(LoggerJson::Text::SYSLOG_JSON)
31
+ require 'syslog_json/logger'
28
32
  end
29
33
 
30
34
  defined?(Syslog::Logger).nil? ? load_logger_json : load_syslog_json
@@ -1,21 +1,3 @@
1
- class Logger
2
- class << self
3
- def json_path
4
- LoggerJson::Text::PATH
5
- end
6
- end
7
-
8
- private
9
-
10
- def create_file(message, type=:info)
11
- path = filename || Logger.json_path
12
- json = LoggerJson::Model.new(message, type).json
13
- LoggerJson::Writter.logger_json(path, json)
14
- end
15
-
16
- def filename
17
- path = instance_variable_get("@logdev").filename
18
- return if path.nil?
19
- path.gsub(".log","-json.log")
20
- end
1
+ Logger.class_eval do
2
+ include LoggerJsonCore
21
3
  end
@@ -12,14 +12,13 @@ module LoggerJson
12
12
  def convert_to_json(msg)
13
13
  return log(msg[0]).to_json if msg[1].nil?
14
14
  return (log(msg[0]).merge(msg[1])).to_json if msg[1].is_a? Hash
15
- return add_array_in_log msg if msg[1].is_a? Array
16
- add_string_in_log msg
15
+ add_in_log msg
17
16
  end
18
17
 
19
18
  def log(message)
20
19
  {
21
20
  date: DateTime.now.iso8601(9),
22
- host: IO.popen('hostname').readlines[0].strip,
21
+ host: host.nil? ? "*" : host.ip_address,
23
22
  thread: Thread.current,
24
23
  methods: methods,
25
24
  level: @type,
@@ -41,10 +40,14 @@ module LoggerJson
41
40
  array.drop(5)
42
41
  end
43
42
 
44
- def add_string_in_log(msg)
43
+ def add_in_log(msg)
45
44
  logs = log msg[0]
46
45
  logs[:log] = msg[1]
47
46
  logs.to_json
48
47
  end
48
+
49
+ def host
50
+ Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
51
+ end
49
52
  end
50
- end
53
+ end
@@ -1,6 +1,7 @@
1
1
  module LoggerJson
2
2
  module Text
3
3
  LOGGER_JSON = 'From now on every log will generate a json'.freeze
4
+ SYSLOG_JSON = 'From now on every syslog will generate a json'.freeze
4
5
  PATH = nil
5
6
  end
6
7
  end
@@ -1,3 +1,3 @@
1
1
  module LoggerJson
2
- VERSION = "1.0.5".freeze
2
+ VERSION = "1.1.0".freeze
3
3
  end
@@ -0,0 +1,5 @@
1
+ module Syslog
2
+ Logger.class_eval do
3
+ include LoggerJsonCore
4
+ end
5
+ end
data/logger_json.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.email = ["vfoliveira@rederecord.com.br"]
13
13
 
14
14
  spec.summary = "Flexible Json Logger solution for Ruby"
15
- spec.description = "LoggerJson is a gem written in Ruby that adds new functionality, without changing the function of the Logger class methods, which creates the logs in json format."
15
+ spec.description = "LoggerJson is a gem written in Ruby that adds new functionality, without changing the function of the Logger and Syslog::Logger class methods, which creates the logs in json format."
16
16
  spec.homepage = "https://gitlab.ir7.com.br/r7/logger-json"
17
17
  spec.license = "MIT"
18
18
  spec.required_ruby_version = '>= 1.9.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logger_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vinicius Freire
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-06 00:00:00.000000000 Z
11
+ date: 2020-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -53,8 +53,8 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
55
  description: LoggerJson is a gem written in Ruby that adds new functionality, without
56
- changing the function of the Logger class methods, which creates the logs in json
57
- format.
56
+ changing the function of the Logger and Syslog::Logger class methods, which creates
57
+ the logs in json format.
58
58
  email:
59
59
  - vfoliveira@rederecord.com.br
60
60
  executables: []
@@ -69,14 +69,14 @@ files:
69
69
  - README.md
70
70
  - bin/console
71
71
  - bin/setup
72
+ - lib/core/logger_json_core.rb
72
73
  - lib/logger_json.rb
73
- - lib/logger_json/json/logger.rb
74
- - lib/logger_json/json/model.rb
75
74
  - lib/logger_json/logger.rb
75
+ - lib/logger_json/model.rb
76
76
  - lib/logger_json/text.rb
77
77
  - lib/logger_json/version.rb
78
78
  - lib/logger_json/writter.rb
79
- - lib/syslog_json/text.rb
79
+ - lib/syslog_json/logger.rb
80
80
  - logger_json.gemspec
81
81
  homepage: https://gitlab.ir7.com.br/r7/logger-json
82
82
  licenses:
@@ -1,11 +0,0 @@
1
- class Logger
2
- %w{debug info warn error fatal}.each do |level|
3
- alias_method "old_#{level}", level
4
-
5
- define_method level do |*args, &block|
6
- create_file args, level
7
- return send("old_#{level}", args[0]) { block.call } unless block.nil?
8
- send "old_#{level}", args[0]
9
- end
10
- end
11
- end
@@ -1,6 +0,0 @@
1
- module Syslog
2
- module Text
3
- SYSLOG_JSON = 'From now on every syslog will generate a json'.freeze
4
- PATH = nil
5
- end
6
- end