File Coverage

blib/lib/Mojo/Log/Role/Color.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             package Mojo::Log::Role::Color;
2 3     3   3616 use Mojo::Base -role;
  3         7  
  3         22  
3              
4 3     3   3413 use Term::ANSIColor ();
  3         26161  
  3         1020  
5              
6             our $VERSION = '0.02';
7              
8             our %COLORS = (
9             debug => ['cyan'],
10             error => ['red'],
11             fatal => ['white on_red'],
12             info => ['green'],
13             warn => ['yellow'],
14             );
15              
16             has colored => sub { $ENV{MOJO_LOG_COLORS} // -t shift->handle };
17              
18             around format => sub {
19             my ($next, $self) = (shift, shift);
20              
21             # set
22             return $next->($self, @_) if @_;
23              
24             # get
25             my $formatter = $next->($self);
26             return $formatter unless $self->colored;
27              
28             return sub {
29             my $level = $_[1];
30             my $message = $formatter->(@_);
31             my $newline = $message =~ s!(\r?\n)$!! ? $1 : '';
32             return Term::ANSIColor::colored($COLORS{$level} || $COLORS{debug}, $message)
33             . $newline;
34             };
35             };
36              
37             1;
38              
39             =encoding utf8
40              
41             =head1 NAME
42              
43             Mojo::Log::Role::Color - Add colors to your mojo logs
44              
45             =head1 SYNOPSIS
46              
47             use Mojo::Log;
48             my $log = Mojo::Log->with_roles("+Color")->new;
49             $log->info("FYI: it happened again");
50              
51             =head1 DESCRIPTION
52              
53             L is a role you can apply to your L to get
54             colored log messages when running your application in interactive mode.
55              
56             It is also possible to set the C environment variable to force
57             colored output.
58              
59             The coloring is based on the log level:
60              
61             debug: cyan text
62             info: green text
63             warn: yellow text
64             error: red text
65             fatal: white text on red background
66              
67             The colors can be customized by changing C<%Mojo::Log::Role::Color::COLORS>,
68             though this is not officially supported, and may break in a future release.
69              
70             =head1 ATTRIBUTES
71              
72             =head2 colored
73              
74             $bool = $log->colored;
75             $log = $log->colored(1);
76              
77             Check if colored output is enabled, or force it to a given state. Defaults to
78             C environment variable, or will be set to "1" if
79             L is attached to a terminal.
80              
81             =head1 AUTHOR
82              
83             Jan Henning Thorsen
84              
85             =head1 COPYRIGHT AND LICENSE
86              
87             Copyright (C) Jan Henning Thorsen
88              
89             This program is free software, you can redistribute it and/or modify it under
90             the terms of the Artistic License version 2.0.
91              
92             =head1 SEE ALSO
93              
94             L.
95              
96             =cut