logeasy 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,39 @@
1
+ /*
2
+ Simple css file for HTML logs.
3
+ While selecting a font, choose one that is mono-spaced.
4
+ Using white-space: pre; is strongly recommended since the log level is aligned using spaces.
5
+ */
6
+
7
+ /* Recommend using a mono-spaced font. */
8
+ body {
9
+ font-family: "Consolas", sans-serif;
10
+ font-size: 1em;
11
+ }
12
+
13
+ body.log {
14
+ font-size: 85%;
15
+ }
16
+
17
+ /* Recommended. Ensures that the log levels (and unformatted messages) are properly aligned. */
18
+ span {
19
+ white-space: pre;
20
+ }
21
+
22
+ span.time {
23
+ font-family: "Courier New", sans-serif;
24
+ }
25
+
26
+ span.WARN {
27
+ background-color: #ff6600;
28
+ color: #ffffff;
29
+ }
30
+
31
+ span.ERROR {
32
+ background-color: #ff0000;
33
+ color: #ffffff;
34
+ }
35
+
36
+ span.FATAL {
37
+ background-color:#660000;
38
+ color: #ffffff;
39
+ }
@@ -31,12 +31,14 @@ end
31
31
 
32
32
  file_appender_2 = LogEasy::FileAppender.new("easylog_example.log") # Simple file logger.
33
33
  file_appender_2.min_level = LogEasy::Level::INFO # Specific level of INFO for the appender. The logger's level will not be applied to this appender now.
34
- console_appender_2 = LogEasy::ConsoleAppender.new # Simple console appender.
35
- console_appender_2.formatter = custom_formatter
36
- file_appender_3 = LogEasy::HTMLFileAppender.new("easylog_example.html", "easylog.css")
37
- file_appender_3.formatter = LogEasy::Formatters::STYLED_HTML_FORMATTER
34
+
35
+ console_appender_1 = LogEasy::ConsoleAppender.new # Simple console appender.
36
+ console_appender_1.formatter = custom_formatter
37
+
38
+ file_appender_3 = LogEasy::HTMLFileAppender.new("easylog_example.html", "w", "easylog.css") # New HTML appender. Uses the STYLED_HTML_FORMATTER by default.
39
+
38
40
  root_logger = LogEasy::Logger.root_logger # Get the handle to the root logger. The root logger's default level is ALL.
39
- root_logger.add_appender(console_appender_2) # Add the appenders to the root logger.
41
+ root_logger.add_appender(console_appender_1) # Add the appenders to the root logger.
40
42
  root_logger.add_appender(file_appender_2)
41
43
  root_logger.add_appender(file_appender_3)
42
44
 
@@ -51,12 +53,16 @@ custom_logger.info("INFO message")
51
53
  custom_logger.warn("WARN message")
52
54
  custom_logger.error("ERROR message")
53
55
  custom_logger.fatal("FATAL message")
54
- custom_logger << "UNFORMATTED message"
56
+ custom_logger << "UNFORMATTED message\nIt has multiple lines.\n\tAnd tabs.\nAnd <escaped> characters."
55
57
 
56
58
  # Adding a custom level.
57
59
  custom_level = LogEasy::Level.new("FINER", 500) # ALL < FINER < FINE
58
60
  custom_logger.log(custom_level, "Custom level message")
59
61
 
62
+ # Turn off parent appenders.
63
+ custom_logger.use_parent_logger = false
64
+ custom_logger.error("Not sent to parent.")
65
+
60
66
  file_appender_1.close
61
67
  file_appender_2.close
62
68
  file_appender_3.close
@@ -80,18 +80,22 @@ module LogEasy
80
80
 
81
81
  attr_reader :file
82
82
  attr_reader :file_path
83
+ attr_reader :mode
83
84
 
84
85
  # Creates a new file appender.
85
- # If the specified file already exists, it is overwritten.
86
- def initialize(file_path)
86
+ # By default, existing files are overwritten. Use the mode argument to control this.
87
+ def initialize(file_path, mode = "w")
87
88
  super()
89
+
88
90
  @file_path = file_path
91
+ @mode = mode
92
+
89
93
  create_file
90
94
  end
91
95
 
92
96
  # Default create file will overwrite existing files.
93
97
  def create_file
94
- @file = File.open(file_path, "w")
98
+ @file = File.open(file_path, mode)
95
99
  end
96
100
 
97
101
  # Writes the message to the file.
@@ -102,32 +106,29 @@ module LogEasy
102
106
 
103
107
  end
104
108
 
105
- # A file appender that writes to the end of the file if it exists.
106
- class ConcatFileAppender < FileAppender
107
-
108
- private
109
-
110
- # Overrides default create file. This will use an existing file.
111
- def create_file
112
- @file = File.open(file_path, "a")
113
- end
114
-
115
- end
116
-
109
+ # A simple HTML file appender.
117
110
  class HTMLFileAppender < FileAppender
118
111
 
119
112
  # Close the file.
120
113
  def close
121
- @file.puts("</body>")
122
- @file.puts("</html>")
114
+ file.puts("</body>")
115
+ file.puts("\n</html>")
116
+
123
117
  # Close the file.
124
118
  super
125
119
  end
126
120
 
127
- # Override do_log to add a line feed to the end of an unformatted message.
121
+ # Write this log item. Since this is an HTML appender, even unformatted logs are sent to the formatter.
122
+ # If this appender's level is higher than the log item's level, this method will return immediately.
123
+ #
124
+ # 'log_item' - The log to write.
128
125
  def do_log(log_item)
129
- log_item.message += "<br />" if log_item.unformatted
130
- super
126
+ return if log_item.level < min_level
127
+ # Escape characters before formatting it.
128
+ escape(log_item)
129
+ # Format the message and log it.
130
+ message = formatter.call(log_item)
131
+ write(message)
131
132
  end
132
133
 
133
134
  private
@@ -135,20 +136,38 @@ module LogEasy
135
136
  attr_reader :external_css
136
137
 
137
138
  # New HTML appender. Use in conjunction with a STYLED_HTML_FORMATTER for best results.
138
- def initialize(file_path, external_css = "log.css")
139
+ # STYLED_HTML_FORMATTER is the default formatter.
140
+ #
141
+ # By default the style sheet referenced is named log.css. Use the external_css argument to control this.
142
+ # By default existing files are overwritten. Use the mode argument to control this.
143
+ def initialize(file_path, mode = "w", external_css = "log.css")
139
144
  @external_css = external_css
140
145
  # Create the appender.
141
- super(file_path)
146
+ super(file_path, mode)
147
+ # The default formatter is the Styled HTML Formatter.
148
+ self.formatter = Formatters::STYLED_HTML_FORMATTER
142
149
  end
143
150
 
144
151
  # Create the file. Adds the header details.
145
152
  def create_file
146
- @file = File.open(file_path, "w")
147
- @file.puts("<html>")
148
- @file.puts("<head>")
149
- @file.puts("<link rel=\"stylesheet\" href=\"#{external_css}\">")
150
- @file.puts("</head>")
151
- @file.puts("<body>")
153
+ # Create the file.
154
+ super
155
+
156
+ file.puts("<html>")
157
+ file.puts("<head><link rel=\"stylesheet\" href=\"#{external_css}\"></head>")
158
+ file.puts("\n<body class=\"log\">")
159
+ end
160
+
161
+ # Escapes all '<' and '>' characters.
162
+ def escape(log_item)
163
+ log_item.message = log_item.message.gsub(/<|>/) do |match|
164
+ case match
165
+ when "<"
166
+ next "&lt;"
167
+ when ">"
168
+ next "&gt;"
169
+ end
170
+ end
152
171
  end
153
172
 
154
173
  end
@@ -3,6 +3,8 @@
3
3
  # 24-02-2011
4
4
  #
5
5
 
6
+ require "logeasy/level"
7
+
6
8
  module LogEasy
7
9
 
8
10
  # Pre-defined formatter procs.
@@ -39,14 +41,20 @@ module LogEasy
39
41
  # The level is wrapped in a <span class="level"> tag.
40
42
  # The logger name is wrapped in a <span class="logger"> tag.
41
43
  # The message is wrapped in a <span class="message"> tag.
44
+ #
45
+ # Unformatted messages are wrapped in a <span class="UNF"> tag, irrespective of the level they were logged at.
42
46
  STYLED_HTML_FORMATTER = proc do |log_item|
43
- level_str = log_item.level.to_s
44
- logger_name = "<span class=\"logger\">(#{log_item.logger.name})</span>"
45
- level = "<span class=\"level\">#{("%-5s" % level_str).gsub(" ", "&nbsp;")}</span>"
46
- message = "<span class=\"message\">#{log_item.message}</span>"
47
- timestamp = "<span class=\"time\">[#{log_item.timestamp.strftime(LOG_TIMESTAMP)}]</span>"
48
-
49
- "<span class=\"#{log_item.level}\">#{timestamp} #{level} -- #{logger_name} #{message}</span><br />"
47
+ if log_item.unformatted
48
+ level_str = Level::UNFORMATTED.to_s
49
+ "<span class=\"#{level_str}\">#{log_item.message}</span><br />"
50
+ else
51
+ level_str = log_item.level.to_s
52
+ logger_name = "<span class=\"logger\">(#{log_item.logger.name})</span>"
53
+ level = "<span class=\"level\">#{("%-5s" % level_str)}</span>"
54
+ message = "<span class=\"message\">#{log_item.message}</span>"
55
+ timestamp = "<span class=\"time\">[#{log_item.timestamp.strftime(LOG_TIMESTAMP)}]</span>"
56
+ "<span class=\"#{level_str}\">#{timestamp} #{level} -- #{logger_name} #{message}</span><br />"
57
+ end
50
58
  end
51
59
 
52
60
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logeasy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-02-24 00:00:00.000000000 +05:30
12
+ date: 2011-02-26 00:00:00.000000000 +05:30
13
13
  default_executable:
14
14
  dependencies: []
15
15
  description: Hierarchical logger for Ruby. The loggers are arranged in a tree. Output
@@ -26,6 +26,7 @@ files:
26
26
  - lib/logeasy/log.rb
27
27
  - lib/logeasy/logger.rb
28
28
  - lib/example/example.rb
29
+ - lib/example/easylog.css
29
30
  has_rdoc: true
30
31
  homepage: http://rubyforge.org/projects/logeasy/
31
32
  licenses: []