Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added %L key to patternformatter which generates colorized log level #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/log4r/formatter/patternformatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class PatternFormatter < BasicFormatter
# %p - process ID aka PID<br>
# %M - formatted message<br>
# %l - Level in string form<br>
# %L - Level in string form colorized<br>
# %x - Nested Diagnostic Context (NDC)<br>
# %X - Mapped Diagnostic Context (MDC), syntax is "%X{key}"<br>
# %% - Insert a %<br>
Expand All @@ -39,6 +40,16 @@ class PatternFormatter < BasicFormatter
"p" => 'Process.pid.to_s',
"M" => 'format_object(event.data)',
"l" => 'LNAMES[event.level]',
"L" => %q|case LNAMES[event.level]
when "ERROR"
"\e[31m#{LNAMES[event.level][0..3]}\e[0m"
when "WARN"
"\e[33m#{LNAMES[event.level][0..3]}\e[0m"
when "INFO"
"\e[32m#{LNAMES[event.level][0..3]}\e[0m"
else
LNAMES[event.level][0..3]
end|,
"x" => 'Log4r::NDC.get()',
"X" => 'Log4r::MDC.get("DTR_REPLACE")',
"%" => '"%"'
Expand All @@ -54,7 +65,7 @@ class PatternFormatter < BasicFormatter
# * $6 is the stuff after the directive or "" if not applicable
# * $7 is the remainder

DirectiveRegexp = /([^%]*)((%-?\d*(\.\d+)?)([cCdgtTmhpMlxX%]))?(\{.+?\})?(.*)/
DirectiveRegexp = /([^%]*)((%-?\d*(\.\d+)?)([cCdgtTmhpMlLxX%]))?(\{.+?\})?(.*)/

# default date format
ISO8601 = "%Y-%m-%d %H:%M:%S"
Expand Down
1 change: 1 addition & 0 deletions lib/log4r/rdoc/patternformatter
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ terminology.
BasicFormatter. It will pretty-print Exceptions, print Strings
and inspect everything else. Slow.
[<b>l</b>] The name of the level. That's l as in Lambda. Fast.
[<b>L</b>] The name of the level colorized and truncated to first 4 characters. Fast.
[<b>%</b>] %% just prints a %. Any formatting is <em>probably</em> ignored.
Fast.

Expand Down