Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to encode JSON hash to UTF-8 #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
language: perl

perl:
- "blead"
- "5.24"
- "5.22"
- "5.20"
- "5.18"
- "5.16"
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

Expand Down
10 changes: 10 additions & 0 deletions lib/Log/Log4perl/Layout/JSON.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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} = {
Expand Down Expand Up @@ -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
)) {
Expand Down
26 changes: 26 additions & 0 deletions t/10-basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use Test::Most;

use Log::Log4perl;

use utf8;
use Encode;

subtest "no mdc" => sub {

Expand Down Expand Up @@ -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();