logeasy 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,8 @@
3
3
  # 19-02-2011
4
4
  #
5
5
 
6
+ $LOAD_PATH << "../"
7
+
6
8
  require "logeasy"
7
9
 
8
10
  # Configuring the self logger. The self logger will log internal logger messages.
@@ -31,9 +33,13 @@ file_appender_2 = LogEasy::FileAppender.new("easylog_example.log") # Simple file
31
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.
32
34
  console_appender_2 = LogEasy::ConsoleAppender.new # Simple console appender.
33
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
38
  root_logger = LogEasy::Logger.root_logger # Get the handle to the root logger. The root logger's default level is ALL.
35
39
  root_logger.add_appender(console_appender_2) # Add the appenders to the root logger.
36
40
  root_logger.add_appender(file_appender_2)
41
+ root_logger.add_appender(file_appender_3)
42
+
37
43
 
38
44
  # Logging to a child logger, sends messages to the parent loggers too (by default).
39
45
  custom_logger = LogEasy::Logger.get_logger("Parent::Child") # Creates a logger. The '::' or '.' characters can be used to separate logger names.
@@ -50,3 +56,7 @@ custom_logger << "UNFORMATTED message"
50
56
  # Adding a custom level.
51
57
  custom_level = LogEasy::Level.new("FINER", 500) # ALL < FINER < FINE
52
58
  custom_logger.log(custom_level, "Custom level message")
59
+
60
+ file_appender_1.close
61
+ file_appender_2.close
62
+ file_appender_3.close
@@ -4,6 +4,7 @@
4
4
  #
5
5
 
6
6
  require "time"
7
+ require "logeasy/formatters"
7
8
 
8
9
  module LogEasy
9
10
 
@@ -12,29 +13,6 @@ module LogEasy
12
13
  # A simple console logger and file logger are provided.
13
14
  class Appender
14
15
 
15
- # Timestamp format for use by log formatters.
16
- LOG_TIMESTAMP = "%d-%m-%Y %H:%M:%S.%L %z"
17
-
18
- # Default formatter that prints messages in the format [TIMESTAMP] [LEVEL] -- [LOGGER] - [MESSAGE]
19
- DEFAULT_FORMATTER = proc do |log_item|
20
- logger_name = log_item.logger.name
21
- level = log_item.level
22
- message = log_item.message
23
- timestamp = log_item.timestamp.strftime(LOG_TIMESTAMP)
24
-
25
- "[#{timestamp}] %-5s -- (%s) - %s\n" % [level, logger_name, message]
26
- end
27
-
28
- # Custom formatter that prints messages in the format [TIMESTAMP] [LEVEL] -- [LOGGER_FULL_NAME] - [MESSAGE]
29
- FULL_LOGGER_NAME_FORMATTER = proc do |log_item|
30
- logger_name = log_item.logger.full_name
31
- level = log_item.level
32
- message = log_item.message
33
- timestamp = log_item.timestamp.strftime(LOG_TIMESTAMP)
34
-
35
- "[#{timestamp}] %-5s -- (%s) - %s\n" % [level, logger_name, message]
36
- end
37
-
38
16
  attr_accessor :formatter
39
17
  attr_accessor :logger
40
18
  attr_accessor :min_level
@@ -56,7 +34,7 @@ module LogEasy
56
34
 
57
35
  # Creates a new appender.
58
36
  def initialize
59
- @formatter = DEFAULT_FORMATTER
37
+ @formatter = Formatters::DEFAULT_FORMATTER
60
38
  @allow_unformatted_messages = true
61
39
  end
62
40
 
@@ -127,6 +105,8 @@ module LogEasy
127
105
  # A file appender that writes to the end of the file if it exists.
128
106
  class ConcatFileAppender < FileAppender
129
107
 
108
+ private
109
+
130
110
  # Overrides default create file. This will use an existing file.
131
111
  def create_file
132
112
  @file = File.open(file_path, "a")
@@ -134,4 +114,43 @@ module LogEasy
134
114
 
135
115
  end
136
116
 
117
+ class HTMLFileAppender < FileAppender
118
+
119
+ # Close the file.
120
+ def close
121
+ @file.puts("</body>")
122
+ @file.puts("</html>")
123
+ # Close the file.
124
+ super
125
+ end
126
+
127
+ # Override do_log to add a line feed to the end of an unformatted message.
128
+ def do_log(log_item)
129
+ log_item.message += "<br />" if log_item.unformatted
130
+ super
131
+ end
132
+
133
+ private
134
+
135
+ attr_reader :external_css
136
+
137
+ # New HTML appender. Use in conjunction with a STYLED_HTML_FORMATTER for best results.
138
+ def initialize(file_path, external_css = "log.css")
139
+ @external_css = external_css
140
+ # Create the appender.
141
+ super(file_path)
142
+ end
143
+
144
+ # Create the file. Adds the header details.
145
+ 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>")
152
+ end
153
+
154
+ end
155
+
137
156
  end
@@ -0,0 +1,54 @@
1
+ #
2
+ # Suraj Vijayakumar
3
+ # 24-02-2011
4
+ #
5
+
6
+ module LogEasy
7
+
8
+ # Pre-defined formatter procs.
9
+ module Formatters
10
+
11
+ # Timestamp format for use by log formatters.
12
+ LOG_TIMESTAMP = "%d-%m-%Y %H:%M:%S.%L %z"
13
+
14
+ # Default formatter that prints messages in the format [TIMESTAMP] [LEVEL] -- [LOGGER] - [MESSAGE]
15
+ DEFAULT_FORMATTER = proc do |log_item|
16
+ logger_name = log_item.logger.name
17
+ level = log_item.level
18
+ message = log_item.message
19
+ timestamp = log_item.timestamp.strftime(LOG_TIMESTAMP)
20
+
21
+ "[#{timestamp}] %-5s -- (%s) - %s\n" % [level, logger_name, message]
22
+ end
23
+
24
+ # Custom formatter that prints messages in the format [TIMESTAMP] [LEVEL] -- [LOGGER_FULL_NAME] - [MESSAGE]
25
+ FULL_LOGGER_NAME_FORMATTER = proc do |log_item|
26
+ logger_name = log_item.logger.full_name
27
+ level = log_item.level
28
+ message = log_item.message
29
+ timestamp = log_item.timestamp.strftime(LOG_TIMESTAMP)
30
+
31
+ "[#{timestamp}] %-5s -- (%s) - %s\n" % [level, logger_name, message]
32
+ end
33
+
34
+ # Styled HTML formatter. Allows the logs to be formatted easily using a CSS.
35
+ #
36
+ # Each element is applied a class name.
37
+ # The whole log message is wrapped in a <span class="<LEVEL>"> tag.
38
+ # The timestamp is wrapped in a <span class="time"> tag.
39
+ # The level is wrapped in a <span class="level"> tag.
40
+ # The logger name is wrapped in a <span class="logger"> tag.
41
+ # The message is wrapped in a <span class="message"> tag.
42
+ 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 />"
50
+ end
51
+
52
+ end
53
+
54
+ end
data/lib/logeasy/log.rb CHANGED
@@ -8,11 +8,11 @@ module LogEasy
8
8
  # Represents the actual log message. Simply a wrapper for the log information (i.e. level, logger, timestamp, message, etc...).
9
9
  class Log
10
10
 
11
- attr_reader :logger
12
- attr_reader :level
13
- attr_reader :message
14
- attr_reader :timestamp
15
- attr_reader :unformatted
11
+ attr_accessor :logger
12
+ attr_accessor :level
13
+ attr_accessor :message
14
+ attr_accessor :timestamp
15
+ attr_accessor :unformatted
16
16
 
17
17
  # New log item.
18
18
  #
@@ -6,6 +6,7 @@
6
6
  require "logeasy/level"
7
7
  require "logeasy/log"
8
8
  require "logeasy/appender"
9
+ require "logeasy/formatters"
9
10
 
10
11
  module LogEasy
11
12
 
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.6
4
+ version: 0.0.7
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-22 00:00:00.000000000 +05:30
12
+ date: 2011-02-24 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
@@ -21,6 +21,7 @@ extra_rdoc_files: []
21
21
  files:
22
22
  - lib/logeasy.rb
23
23
  - lib/logeasy/appender.rb
24
+ - lib/logeasy/formatters.rb
24
25
  - lib/logeasy/level.rb
25
26
  - lib/logeasy/log.rb
26
27
  - lib/logeasy/logger.rb