| 1 | package Dancer2::Logger::Console::Colored; | ||
| 2 | # ABSTRACT: Dancer2 colored console logger. | ||
| 3 | # | ||
| 4 | # This file is part of Dancer2-Logger-Console-Colored | ||
| 5 | # | ||
| 6 | # This software is Copyright (c) 2014 by BURNERSK <burnersk@cpan.org>. | ||
| 7 | # | ||
| 8 | # This is free software, licensed under: | ||
| 9 | # | ||
| 10 | # The MIT (X11) License | ||
| 11 | # | ||
| 12 | |||
| 13 | BEGIN { | ||
| 14 | our $VERSION = '0.001'; # VERSION: generated by DZP::OurPkgVersion | ||
| 15 | } | ||
| 16 | |||
| 17 | use Term::ANSIColor; | ||
| 18 | use Moo; | ||
| 19 | use Dancer2::Core::Types qw( HashRef Str ); | ||
| 20 | |||
| 21 | extends 'Dancer2::Logger::Console'; | ||
| 22 | |||
| 23 | has colored_origin => ( | ||
| 24 | is => 'rw', | ||
| 25 | isa => Str, | ||
| 26 | ); | ||
| 27 | |||
| 28 | has colored_levels => ( | ||
| 29 | is => 'rw', | ||
| 30 | isa => HashRef [Str], | ||
| 31 | default => sub { {} }, | ||
| 32 | ); | ||
| 33 | |||
| 34 | has colored_messages => ( | ||
| 35 | is => 'rw', | ||
| 36 | isa => HashRef [Str], | ||
| 37 | default => sub { {} }, | ||
| 38 | ); | ||
| 39 | |||
| 40 | sub colorize_origin { | ||
| 41 | my ( $self, $string ) = @_; | ||
| 42 | |||
| 43 | # Configured color. | ||
| 44 | return colored( $string, $self->colored_origin ) if $self->colored_origin; | ||
| 45 | |||
| 46 | # Default colors. | ||
| 47 | return colored( $string, 'cyan' ); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub colorize_level { | ||
| 51 | my ( $self, $level ) = @_; | ||
| 52 | my $level_tmp = $level =~ s/\s+//gr; | ||
| 53 | $level_tmp = 'warning' if $level_tmp eq 'warn'; | ||
| 54 | |||
| 55 | # Configured color. | ||
| 56 | return colored( $level, $self->colored_levels->{$level_tmp} ) if $self->colored_levels->{$level_tmp}; | ||
| 57 | |||
| 58 | # Default colors. | ||
| 59 | return colored( $level, 'bold bright_white' ) if $level_tmp eq 'core'; | ||
| 60 | return colored( $level, 'bold bright_blue' ) if $level_tmp eq 'debug'; | ||
| 61 | return colored( $level, 'bold green' ) if $level_tmp eq 'info'; | ||
| 62 | return colored( $level, 'bold yellow' ) if $level_tmp eq 'warning'; | ||
| 63 | return colored( $level, 'bold yellow on_red' ) if $level_tmp eq 'error'; | ||
| 64 | return colored( $level, 'bold magenta' ); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub colorize_message { | ||
| 68 | my ( $self, $level, $message ) = @_; | ||
| 69 | my $level_tmp = $level =~ s/\s+//gr; | ||
| 70 | $level_tmp = 'warning' if $level_tmp eq 'warn'; | ||
| 71 | |||
| 72 | # Configured color. | ||
| 73 | return colored( $message, $self->colored_messages->{$level_tmp} ) if $self->colored_messages->{$level_tmp}; | ||
| 74 | |||
| 75 | # Default colors. | ||
| 76 | return colored( $message, 'bold bright_white' ) if $level_tmp eq 'core'; | ||
| 77 | return colored( $message, 'bold bright_blue' ) if $level_tmp eq 'debug'; | ||
| 78 | return colored( $message, 'bold green' ) if $level_tmp eq 'info'; | ||
| 79 | return colored( $message, 'bold yellow' ) if $level_tmp eq 'warning'; | ||
| 80 | return colored( $message, 'bold yellow on_red' ) if $level_tmp eq 'error'; | ||
| 81 | return colored( $message, 'bold magenta' ); | ||
| 82 | } | ||
| 83 | |||
| 84 | # This comes original from Dancer2::Logger::Console. There are a few hooks | ||
| 85 | # required in order to colorize log messages propably. | ||
| 86 | sub format_message { | ||
| 87 | my ( $self, $level, $message ) = @_; | ||
| 88 | chomp $message; | ||
| 89 | |||
| 90 | $level = sprintf( '%5s', $level ); | ||
| 91 | $message = Encode::encode( $self->auto_encoding_charset, $message ) | ||
| 92 | if $self->auto_encoding_charset; | ||
| 93 | |||
| 94 | my @stack = caller(6); | ||
| 95 | |||
| 96 | my $block_handler = sub { | ||
| 97 | my ( $block, $type ) = @_; | ||
| 98 | if ( $type eq 't' ) { | ||
| 99 | return "[" . strftime( $block, localtime(time) ) . "]"; | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | Carp::carp("{$block}$type not supported"); | ||
| 103 | return "-"; | ||
| 104 | } | ||
| 105 | }; | ||
| 106 | |||
| 107 | my $chars_mapping = { | ||
| 108 | a => sub { $self->colorize_origin( $self->app_name ) }, | ||
| 109 | t => sub { | ||
| 110 | Encode::decode( | ||
| 111 | setting('charset'), | ||
| 112 | POSIX::strftime( "%d/%b/%Y %H:%M:%S", localtime(time) ) ); | ||
| 113 | }, | ||
| 114 | T => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime(time) ) }, | ||
| 115 | P => sub { $$ }, | ||
| 116 | L => sub { $self->colorize_level($level) }, | ||
| 117 | m => sub { $self->colorize_message( $level => $message ) }, | ||
| 118 | f => sub { $self->colorize_origin( $stack[1] || '-' ) }, | ||
| 119 | l => sub { $self->colorize_origin( $stack[2] || '-' ) }, | ||
| 120 | }; | ||
| 121 | |||
| 122 | my $char_mapping = sub { | ||
| 123 | my $char = shift; | ||
| 124 | |||
| 125 | my $cb = $chars_mapping->{$char}; | ||
| 126 | if ( !$cb ) { | ||
| 127 | Carp::carp "\%$char not supported."; | ||
| 128 | return "-"; | ||
| 129 | } | ||
| 130 | $cb->($char); | ||
| 131 | }; | ||
| 132 | |||
| 133 | my $fmt = $self->log_format; | ||
| 134 | |||
| 135 | $fmt =~ s/ | ||
| 136 | (?: | ||
| 137 | \%\{(.+?)\}([a-z])| | ||
| 138 | \%([a-zA-Z]) | ||
| 139 | ) | ||
| 140 | / $1 ? $block_handler->($1, $2) : $char_mapping->($3) /egx; | ||
| 141 | |||
| 142 | return $fmt . "\n"; | ||
| 143 | } | ||
| 144 | |||
| 145 | 1; | ||
| 146 | |||
| 147 | __END__ |