Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
improve Mojo::Log to use native systemd log levels
  • Loading branch information
kraih committed Feb 6, 2018
1 parent d471608 commit 756b3cf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

7.63 2018-02-06
- Improved Mojo::Log to use native systemd log levels.

7.62 2018-02-01
- Added -u option to get command. (jberger)
Expand Down
8 changes: 7 additions & 1 deletion lib/Mojo/Log.pm
Expand Up @@ -24,6 +24,9 @@ has short => sub { $ENV{MOJO_LOG_SHORT} };
# Supported log levels
my %LEVEL = (debug => 1, info => 2, warn => 3, error => 4, fatal => 5);

# Systemd magic numbers
my %MAGIC = (debug => 7, info => 6, warn => 4, error => 3, fatal => 2);

sub append {
my ($self, $msg) = @_;

Expand Down Expand Up @@ -67,7 +70,10 @@ sub _message {
$self->append($self->format->(@$msg));
}

sub _short { shift; '[' . shift() . '] ' . join "\n", @_, '' }
sub _short {
my ($time, $level) = (shift, shift);
return "<$MAGIC{$level}>[$level] " . join "\n", @_, '';
}

1;

Expand Down
10 changes: 9 additions & 1 deletion t/mojo/log.t
Expand Up @@ -61,8 +61,16 @@ like $log->format->(time, 'debug', qw(Test 1 2 3)), qr/^debug:\d+:Test:1:2:3$/,
ok $log->short, 'short messages';
$log = Mojo::Log->new(short => 1);
ok $log->short, 'short messages';
like $log->format->(time, 'debug', 'Test 123'), qr/^\[debug\] Test 123\n$/,
like $log->format->(time, 'debug', 'Test 123'),
qr/^<7>\[debug\] Test 123\n$/, 'right format';
like $log->format->(time, 'info', 'Test 123'), qr/^<6>\[info\] Test 123\n$/,
'right format';
like $log->format->(time, 'warn', 'Test 123'), qr/^<4>\[warn\] Test 123\n$/,
'right format';
like $log->format->(time, 'error', 'Test 123'),
qr/^<3>\[error\] Test 123\n$/, 'right format';
like $log->format->(time, 'fatal', 'Test 123'),
qr/^<2>\[fatal\] Test 123\n$/, 'right format';
}

# Events
Expand Down

0 comments on commit 756b3cf

Please sign in to comment.