diff --git a/.travis.yml b/.travis.yml index a82f196..9ce4e91 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,8 @@ language: perl perl: - - "blead" + - "5.24" + - "5.22" - "5.20" - "5.18" - "5.16" diff --git a/README.md b/README.md index eb53321..85e49bb 100644 --- a/README.md +++ b/README.md @@ -29,18 +29,18 @@ Example configuration: log4perl.appender.Test.layout.prefix = @cee: - # Include the data in the Log::Log4perl::MDC hash (optional) log4perl.appender.Test.layout.include_mdc = 1 # Use this field name for MDC data (else MDC data is placed at top level) log4perl.appender.Test.layout.name_for_mdc = mdc - # Use canonical order for hash keys (optional) - log4perl.appender.Test.layout.canonical = 1 + # Encode the JSON hash to UTF-8 + log4perl.appender.Test.layout.utf8 = 1 + # DESCRIPTION This class implements a "Log::Log4perl" layout format, similar to @@ -49,7 +49,7 @@ hash. The JSON hash is ASCII encoded, with no newlines or other whitespace, and is suitable for output, via Log::Log4perl appenders, to files and -syslog etc. +syslog etc. The JSON hash can, optionally, be UTF-8 encoded. Contextual data in the Log::Log4perl::MDC hash can be included. diff --git a/lib/Log/Log4perl/Layout/JSON.pm b/lib/Log/Log4perl/Layout/JSON.pm index ac4fabd..d7d00b7 100644 --- a/lib/Log/Log4perl/Layout/JSON.pm +++ b/lib/Log/Log4perl/Layout/JSON.pm @@ -105,6 +105,10 @@ field removed. In future this rather dumb logic will be replaced by something smarter. +=head2 utf8 + +Switch JSON encoding from ASCII to UTF-8. + =head2 EXAMPLE USING Log::Log4perl::MDC local Log::Log4perl::MDC->get_context->{request} = { @@ -237,6 +241,12 @@ sub BUILD { ## no critic (RequireArgUnpacking) $self->field(delete $args->{field}) if $args->{field}; + # Optionally override encoding from ascii to utf8 + if (my $arg = $args->{utf8}) { + delete $args->{utf8}; + $self->codec( $self->codec->ascii(0)->utf8(1) ); + } + for my $arg_name (qw( canonical prefix include_mdc name_for_mdc max_json_length_kb )) { diff --git a/t/10-basic.t b/t/10-basic.t index 749d758..7e7e5ac 100644 --- a/t/10-basic.t +++ b/t/10-basic.t @@ -4,6 +4,8 @@ use Test::Most; use Log::Log4perl; +use utf8; +use Encode; subtest "no mdc" => sub { @@ -123,4 +125,28 @@ subtest "without mdc" => sub { $appender->string(''); }; +subtest "utf8_configured_and_non_ascii_data" => sub { + + my $conf = qq( + log4perl.rootLogger = INFO, Test + log4perl.appender.Test = Log::Log4perl::Appender::String + log4perl.appender.Test.layout = Log::Log4perl::Layout::JSON + log4perl.appender.Test.layout.field.message = %m + log4perl.appender.Test.layout.canonical = 1 + log4perl.appender.Test.layout.utf8 = 1 + log4perl.appender.Test.layout.field.category = %c + log4perl.appender.Test.layout.field.class = %C + log4perl.appender.Test.layout.field.file = %F{1} + log4perl.appender.Test.layout.field.sub = %M{1} + ); + Log::Log4perl::init( \$conf ); + + ok my $appender = Log::Log4perl->appender_by_name("Test"); + ok my $logger = Log::Log4perl->get_logger('foo'); + + $logger->info('A string containing ütf8'); + + is_deeply $appender->string(), encode_utf8('{"category":"foo","class":"Log::Log4perl::Logger","file":"Logger.pm","message":"A string containing ütf8","sub":"__ANON__"}')."\n"; +}; + done_testing();