logzomg 0.1.0 → 0.1.0.1

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
  SHA1:
3
- metadata.gz: c62b72556a339925393753589f92925d63347584
4
- data.tar.gz: 11dedeeb8b5f4bc2d36c821aeff053228ab66497
3
+ metadata.gz: aff1c4e5aeec14f295006eb33123760bb05e2787
4
+ data.tar.gz: cf5b53019762166d1072d8fb6f49fc2c9187d515
5
5
  SHA512:
6
- metadata.gz: 8d029c7328cc8ea4373fc1b1bc2c143cd6018a87f4ed10a4e53a963ceb0eccbd0c6d11c02348636f0e1c857fce4b32305b0bbfd842cbdd7480cde596de0de158
7
- data.tar.gz: 43d4917e4f3df95db18d322e672aed5c095d76489bf485e8e0a6b79be654ed3c68253eda029beedba49dc4c18f27d7d8a429de64e98f2113fd694f124830879e
6
+ metadata.gz: b3f3db722dd5bde85de0a881615a4122d6255eae4d22577e68aa503493c51d61685a688740dbce6f311395937929b4cf48dcad8ac4e0e91a26d4cff07d9ec160
7
+ data.tar.gz: 62a13c97056e402e2f86caaac96d098f3a0706e0be4472f2dfa4564009155dc3308e27bd1552762845e232bc2fe348e6bf59c84b971bb0d9af12a0e771505eec
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  An easy to use and lightweight logging gem for Ruby.
6
6
 
7
- ![Terminal output](http://i.imgur.com/reu3xof.png)
7
+ ![Terminal output](http://i.imgur.com/dsQyWNo.png)
8
8
 
9
9
  Logzomg currently supports varying levels of severity.
10
10
 
@@ -26,26 +26,40 @@ Fatal
26
26
 
27
27
  ## Usage
28
28
 
29
+ The logger takes a hash in and formats the key-value pairs. The key msg will always be displayed last.
30
+
29
31
  You can instantiate the logger and use it like so:
30
32
 
31
33
  ```ruby
32
34
  def log
33
35
  l = Logzomg::Logger.new
34
36
  l.log({
35
- "msg": "Frank took the last coffee and forgot to make some new. Fire him?",
36
- "level": "info", # Optional. Sets log level. Default is warning
37
- "file": "frank.txt" # Optional. Sets name of file to log to. Default is log.txt
37
+ number: 3,
38
+ entity: "Acro-yoga enthusiasts",
39
+ wild: true,
40
+ msg: "Have been spotted!",
41
+ damn: "son",
42
+ level: "info", # Optional. Sets log level. Default is warning
43
+ file: "help.txt" # Optional. Sets name of file to log to. Default is log.txt
38
44
  })
39
45
  end
40
46
  ```
41
47
 
48
+ Giving this result:
49
+ ![Terminal output](http://i.imgur.com/IvprMBS.png)
50
+
42
51
  By default Logzomg color codes the logging levels when viewed in a terminal. If you want to turn this off you can instead pass a TextFormatter when instantiating the logger.
43
52
 
44
53
  The TextFormatter takes a hash of options.
45
54
 
46
- `
47
- Logzomg::Logger.new(TextFormatter.new({with_colors: false}))
48
- `
55
+ ```ruby
56
+ def logger
57
+ Logzomg::Logger.new(formatter: TextFormatter.new { |f|
58
+ f.with_color = true
59
+ f.short_date = true
60
+ })
61
+ end
62
+ ```
49
63
 
50
64
  Currently Logzomg only supports logging as plaintext. Soon it will support logging as JSON as well.
51
65
 
@@ -4,27 +4,57 @@ require 'date'
4
4
  # Allows color output to logfile
5
5
 
6
6
  class TextFormatter
7
+ MAX_CHAR = 150
8
+ MAX_CHAR_FLOAT = 145.00 # There's some problems with float decimal precision. Keep this 5 below MAX_CHAR
9
+ COLORS = {
10
+ info: "96",
11
+ warning: "33",
12
+ debug: "34",
13
+ error: "91",
14
+ fatal: "31"
15
+ }
7
16
 
8
- def initialize(init_hash = Hash.new)
9
- @color = init_hash.has_key?(:with_color) ? init_hash[:with_color] : true
10
- puts "Init color: " + @color.to_s
17
+ attr_accessor :with_color, :short_date
18
+
19
+ def initialize
20
+ if block_given?
21
+ yield(self)
22
+ end
11
23
  end
12
24
 
13
25
  # Split the message into 145 char chunks.
14
26
  # Prepend level. Append DateTime
15
- def format_msg(msg, level)
16
- @level = level
17
- str = ""
18
- s_msg = split_msg(msg) # Split message incase it's 150+ chars
19
- count = 0 # Use to indent on 2nd+ iteration
27
+ def format_msg(hash, level)
28
+ @level = level # Log level
29
+ str = "" # Final String we return. This is the one we add to
30
+ str_keys_values = "" # One long String in format "key: value | key2: value2"
31
+ s_msg = [] # Each index in this Array is the maximum allowed amount of chars on the screen
32
+
33
+ str_keys_values += hash[:msg].to_s + " | " # Add msg first
34
+
35
+ # We loop over every key in the hash and format the key/value in the string format we want
36
+ hash.each do |key, value|
37
+ if !key.to_s.eql?("level") && !key.to_s.eql?("msg") && !key.to_s.eql?("file")
38
+ key = "#{add_color_key(key)} :" if with_color # Add color to keys if with_color is set to true
39
+ str_keys_values += "#{key}: #{value} "
40
+ end
41
+ end
42
+
43
+ s_msg = split_msg(str_keys_values) # Split message incase it's MAX_CHAR+ chars
20
44
 
45
+ s_msg.each do |n|
46
+ puts n
47
+ end
48
+
49
+ count = 0 # Use to indent on 2nd+ iteration
50
+
21
51
  s_msg.each do |n|
22
52
  str += add_msg_identifier(str)
23
- text = count >= 1 ? " " + n : n # Indent if 2nd+ iteration
53
+ text = count >= 1 ? " " + n : n # Indent if 2nd+ iteration
24
54
  str += text
25
55
  indented = count >= 1 # Set indented to determine space amount
26
- str += right_align_date(n, indented)
27
- str += " | " + DateTime.now.to_s + "\n"
56
+ str += right_align_date(n, hash, count+1, s_msg)
57
+ str += " | " + add_date(count) + "\n"
28
58
  count += 1
29
59
  end
30
60
 
@@ -33,49 +63,70 @@ class TextFormatter
33
63
 
34
64
 
35
65
  private
66
+ def add_date(count)
67
+ date = short_date ? DateTime.now.strftime('%b-%e %H:%M:%S') : DateTime.now.to_s
68
+ count > 0 ? "" : date
69
+ end
70
+
36
71
  # Adds spaces to right align date depending on str length and row number
37
- def right_align_date(msg, indented)
38
- indented ? s = 60 : s = 58
39
- " " * (s - msg.length)
72
+ #
73
+ # Since we add colouring to each key we're adding invisible characters
74
+ # Each colour adds 9 extra characters so we have to handle this
75
+ def right_align_date(msg, hash, itr, full_msgs)
76
+ extra = 0
77
+ # If msg is first don't right_align since there's always a line 2
78
+ if with_color
79
+ if msg == full_msgs.first && full_msgs.size > 1 # Triggers on first line of multiline
80
+ extra = 0
81
+ puts "#{msg} #{itr} triggered 1"
82
+ elsif itr == 1 # Triggers on first itr if it's one-line with args
83
+ extra = 9 * get_size(hash)
84
+ puts "#{msg} #{itr} triggered 2"
85
+ elsif itr == full_msgs.length && get_size(hash) > 0 # Triggers on last line of multiline msg
86
+ extra = 9
87
+ puts "#{msg} #{itr} triggered 3"
88
+ end
89
+ end
90
+
91
+ s = itr > 1 ? MAX_CHAR - 2 : MAX_CHAR - 1
92
+ " " * (s - msg.length + extra)
93
+ end
94
+
95
+ # Returns size but without certain keys that wont be displayed
96
+ def get_size(hash)
97
+ size = 0
98
+ hash.keys.each do |n|
99
+ size += 1 unless n.to_s.eql?("file") || n.to_s.eql?("level") || n.to_s.eql?("msg")
100
+ end
101
+ size
40
102
  end
41
103
 
42
104
  # Adds log level to start of log
43
105
  # Add color if color is true
44
106
  def add_msg_identifier(str)
45
- if @level == 'debug'
46
- str += "DBUG"
47
- elsif @level == 'info'
48
- str += "INFO"
49
- elsif @level == 'warning'
50
- str += "WARN"
51
- elsif @level == 'error'
52
- str += "ERRO"
53
- elsif @level == 'fatal'
54
- str += "FATA"
55
- end
56
- str = add_color(str) if @color
107
+ # Need a check since debug has a different identifier than the rest
108
+ str = @level == 'debug' ? 'DBUG' : @level[0..3].upcase
109
+ str = add_color_level(str) if with_color
57
110
  str += + " | "
58
111
  end
59
112
 
60
- # Adds color if color is true
61
- def add_color(str)
62
- colors = {debug: "\e[34m", info: "\e[96m", warning: "\e[33m", error: "\e[91m", fatal: "\e[31m"}
113
+ # Adds color to level if color is true
114
+ def add_color_level(str)
63
115
  close_color = "\e[0m"
64
- str.prepend(colors[@level.to_sym])
116
+ str.prepend("\033[" + COLORS[@level.to_sym] + "m")
65
117
  str += close_color
66
118
  end
67
119
 
68
- # Splits the message every 150 chars to make everything look prettier
120
+ # Adds color to hash key
121
+ def add_color_key(key)
122
+ "\033[" + COLORS[@level.to_sym] + "m" + key.to_s + "\e[0m"
123
+ end
124
+
125
+ # Splits the message
126
+ # Substring using MAX_CHAR - 5 but not if in middle of a word
69
127
  def split_msg(msg)
70
- arr = []
71
- start = 0
72
- ending = 145
73
- c = msg.length > 150 ? ((msg.length).to_f/150.00).ceil : 1
74
- (1..c).each do |n|
75
- arr << msg[start,ending]
76
- start += 146
77
- ending += 145
78
- end
79
- arr
128
+ sub_end = MAX_CHAR - 5
129
+ # Goes back to start of word if matches inside a word. If it matches inside a coloured key go back to start of word before colour
130
+ msg.scan(/.{0,#{Regexp.escape(sub_end.to_s)}}[^\\033\[33m\]a-z\\e\[0m$a-z.!?,;](?:\b|$)/mi)
80
131
  end
81
132
  end
@@ -1,3 +1,3 @@
1
1
  module Logzomg
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.0.1"
3
3
  end
data/lib/logzomg.rb CHANGED
@@ -10,9 +10,14 @@ module Logzomg
10
10
  UnsupportedType = Class.new(StandardError)
11
11
  UnsupportedLevel = Class.new(StandardError)
12
12
 
13
- def initialize(formatter=TextFormatter.new)
14
- @level = 'warning'
15
- @formatter = formatter
13
+ def initialize(**args, &block)
14
+ # Set some default values
15
+ @level = args[:level] ? args[:level] : "warning"
16
+ @formatter = args[:formatter] ? args[:formatter] : TextFormatter.new {|f| f.with_color = true}
17
+ if block_given?
18
+ yield(self)
19
+ return
20
+ end
16
21
  end
17
22
 
18
23
  # First set level ->
@@ -21,7 +26,7 @@ module Logzomg
21
26
  def log(hash)
22
27
  raise UnsupportedType, "Must be of type Hash" unless valid_hash?(hash)
23
28
  level(hash[:level]) if hash.has_key?(:level) # Use default if not included
24
- msg = @formatter.format_msg(hash[:msg], @level)
29
+ msg = @formatter.format_msg(hash, @level)
25
30
  write(hash, msg)
26
31
  end
27
32
 
@@ -34,33 +39,55 @@ module Logzomg
34
39
 
35
40
  # REMOVE AFTER DONE CODING
36
41
  def test
37
- l = Logzomg::Logger.new
38
- l.log({
39
- "msg": "Something is on the horizon!",
40
- "level": "debug"
41
- })
42
- #l.log({
43
- # "msg": "This is a really really really really really really really really really " +
44
- # "really really really really really really really really really really really " +
45
- # "really really really really really really debug message",
46
- # "level": "debug"
47
- # })
48
- l.log({
49
- "msg": "Three wild acro-yoga enthusiasts have been spotted!",
50
- "level": "info"
51
- })
52
- l.log({
53
- "msg": "They are coming closer!",
54
- "level": "warning"
55
- })
56
- l.log({
57
- "msg": "I don\'t know how to handle them!!",
58
- "level": "error"
59
- })
60
- l.log({
61
- "msg": "You have died",
62
- "level": "fatal"
63
- })
42
+ self.log({
43
+ msg: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ipsum dui, luctus ac velit nec, imperdiet elementum turpis." +
44
+ "Pellentesque velit tellus, ultricies non mauris at, semper rhoncus enim. Duis blandit arcu aliquam, sollicitudin leo quis, feugiat orci." +
45
+ "Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed et tortor vel lectus convallis posuere ac vel lacus." +
46
+ "Praesent vestibulum cursus ipsum vel elementum. Fusce elit enim, sollicitudin id sem consequat, molestie accumsan mi. Aliquam vehicula urna tortor," +
47
+ "sit amet laoreet lacus pulvinar eu. Ut imperdiet vitae neque eu consectetur. Nam ac lacus finibus, fringilla neque ac, maximus ligula. Duis ullamcorper," +
48
+ "felis sit amet luctus vehicula, sapien est dictum lorem, nec blandit nunc mi id enim. Curabitur quis laoreet erat. Maecenas varius cursus blandit." +
49
+ "felis sit amet luctus vehicula, sapien est dictum lorem, nec blandit nunc mi id enim. Curabitur quis laoreet erat. Maecenas varius cursus blandit." +
50
+ "felis sit amet luctus vehicula, sapien est dictum lorem, nec blandit nunc mi id enim. Curabitur quis laoreet erat. Maecenas varius cursus blandit." +
51
+ "felis sit amet luctus vehicunec blandit nunc mi id enim. Curabitur quis laoreet erat. Maecenas varius cursus blandit." +
52
+ "felis sit amet luctus vehicula, sapien est dictum lorem, nec blandit nunc mi id enim. Curabitur quis laoreet erat. Maecenas varius cursus blandit." +
53
+ "felis sit amet luctus vehicula, sapien est ditur quis laoreet erat. Maecenas varius cursus blandit." +
54
+ "felis sit amet luctus vehicula, sapien est dictum lorem, nec blandit nunc mi id enim. Curabitur quis laoreet erat. Maecenas varius cursus blandit." +
55
+ "felis sit amet luctus vehicula, sapien est dictum lorem, nec blandit nunc mi id enim. Curabitur quis laoreet erat. Maecenas varius cursus blandit." +
56
+ "felis sit amet luctus vehicula, sapien est dictum lorem, nec blandit nunc mi id enim. Curabitur quis laoreet erat. Maecenas varius cursus blandit." +
57
+ "felis sit amet luctus vehicula, sapien est dictum lorem, nec blandit nunc mi id enim. Curabitur quis laoreet erat. Maecenas varius cursus blandit." +
58
+ "felis sit amet luctus vehicula, sapien est dictum lorem, nec blandit nunc mi id enim. Curabitur quis laoreet erat. Maecenas varius cursus blandit." +
59
+ "Aliquam eleifend mauris ut nisl mollis, quis placerat nisi bibendum.",
60
+ long: true,
61
+ level: "debug"
62
+ })
63
+ self.log({
64
+ msg: "Something is on the horizon!",
65
+ spotted: "unknown",
66
+ level: "debug"
67
+ })
68
+ self.log({
69
+ number: 3,
70
+ entity: "Acro-yoga enthusiasts",
71
+ wild: true,
72
+ msg: "Have been spotted!",
73
+ damn: "son",
74
+ level: "info"
75
+ })
76
+ self.log({
77
+ msg: "They are coming closer!",
78
+ speed: "5km/t",
79
+ level: "warning"
80
+ })
81
+ self.log({
82
+ msg: "I don't know how to handle them!!",
83
+ omg: true,
84
+ level: "error"
85
+ })
86
+ self.log({
87
+ dead: true,
88
+ msg: "You have died",
89
+ level: "fatal"
90
+ })
64
91
  end
65
92
 
66
93
  private
@@ -81,4 +108,4 @@ module Logzomg
81
108
  return !level.nil? && LEVELS.any? { |l| l == level.downcase }
82
109
  end
83
110
  end
84
- end
111
+ end
data/logzomg-0.1.0.gem ADDED
Binary file
data/logzomg.gemspec CHANGED
@@ -8,6 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Logzomg::VERSION
9
9
  spec.authors = ["Rasmus"]
10
10
  spec.email = ["Rasmusbreiler@gmail.com"]
11
+ spec.description = "An easy to use, lightweight logging gem for Ruby applications"
11
12
 
12
13
  spec.summary = %q{Logging gem with Asana API integration}
13
14
  spec.homepage = "https://github.com/Evilbits/logzomg"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logzomg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rasmus
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-01 00:00:00.000000000 Z
11
+ date: 2017-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
- description:
55
+ description: An easy to use, lightweight logging gem for Ruby applications
56
56
  email:
57
57
  - Rasmusbreiler@gmail.com
58
58
  executables: []
@@ -71,6 +71,7 @@ files:
71
71
  - lib/formatters/text_formatter.rb
72
72
  - lib/logzomg.rb
73
73
  - lib/logzomg/version.rb
74
+ - logzomg-0.1.0.gem
74
75
  - logzomg.gemspec
75
76
  homepage: https://github.com/Evilbits/logzomg
76
77
  licenses: