-
Notifications
You must be signed in to change notification settings - Fork 3
/
Colored.pm
152 lines (106 loc) · 3.96 KB
/
Colored.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package Mojo::Log::Colored;
use Mojo::Base 'Mojo::Log';
use Term::ANSIColor 'colored';
use if $^O eq "MSWin32", "Win32::Console::ANSI";
our $VERSION = "0.02";
has 'colors' => sub {
return {
debug => "bold bright_white",
info => "bold bright_blue",
warn => "bold green",
error => "bold yellow",
fatal => "bold yellow on_red",
};
};
has _format => sub {
shift->format( \&_default_format );
};
sub format {
return $_[0]->_format if @_ == 1;
my ( $self, $format ) = @_;
return $self->_format(
sub {
# Prevent having the end escape sequence at the start of the line
local $Term::ANSIColor::EACHLINE = "\n";
# only add colors if we have a color for this level
exists $self->colors->{ $_[1] }
? colored( $format->(@_), $self->colors->{ $_[1] } )
: $format->(@_);
}
)->_format;
}
sub _default_format {
'[' . localtime(shift) . '] [' . shift() . '] ' . join "\n", @_, '';
}
1;
__END__
=encoding utf-8
=head1 NAME
Mojo::Log::Colored - Colored Mojo logging
=begin html
<p>
<a href="https://travis-ci.org/simbabque/Mojo-Log-Colored"><img src="https://travis-ci.org/simbabque/Mojo-Log-Colored.svg?branch=master"></a>
<a href='https://coveralls.io/github/simbabque/Mojo-Log-Colored?branch=master'><img src='https://coveralls.io/repos/github/simbabque/Mojo-Log-Colored/badge.svg?branch=master' alt='Coverage Status' /></a>
</p>
=end html
=head1 SYNOPSIS
use Mojo::Log::Colored;
# Log to STDERR
$app->log(
Mojo::Log::Colored->new(
# optionally set the colors
colors => {
debug => "bold bright_white",
info => "bold bright_blue",
warn => "bold green",
error => "bold yellow",
fatal => "bold yellow on_red",
}
)
);
=head1 DESCRIPTION
Mojo::Log::Colored is a logger for Mojolicious with colored output for the terminal. It lets you define colors
for each log level based on L<Term::ANSIColor> and comes with sensible default colors. The full lines in the log
will be colored.
Since this inherits from L<Mojo::Log> you can still give it a C<file>, but the output would also be colored.
That does not make a lot of sense, so you don't want to do that. Use this for development, not production.
=head1 ATTRIBUTES
L<Mojo::Log::Colored> implements the following attributes.
=head2 colors
my $colors = $log->colors;
$log->colors(
{
debug => "bold bright_white",
info => "bold bright_blue",
warn => "bold green",
error => "bold yellow",
fatal => "bold yellow on_red",
}
);
Takes a hash reference with the five log levels as keys and strings of colors as values. Refer to
L<Term::ANSIColor> for more information about what kind of color you can use.
You can turn off coloring for specific levels by omitting them from the config hash.
$log->colors(
{
fatal => "bold green on_red",
}
);
The above will only color fatal messages. All other levels will be in your default terminal color.
=head2 format
my $cb = $log->format;
$log = $log->format( sub { ... } );
A callback for formatting log messages. Cannot be passed to C<new> at construction! See L<Mojo::Log> for more information.
=head1 METHODS
L<Mojo::Log::Colored> inherits all methods from L<Mojo::Log> and does not implement new ones.
=head1 SEE ALSO
L<Mojo::Log>, L<Term::ANSIColor>
=head1 ACKNOWLEDGEMENTS
This plugin was inspired by lanti asking about a way to find specific errors more easily
in the Mojo log during unit test runs on L<Stack Overflow|https://stackoverflow.com/q/44965998/1331451>.
=head1 LICENSE
Copyright (C) simbabque.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 AUTHOR
simbabque E<lt>simbabque@cpan.orgE<gt>
=cut