Log-Journald-0.30/0000775001762600176260000000000012402074174014166 5ustar lkundraklkundrakLog-Journald-0.30/Build.PL0000444001762600176260000000161712402074174015463 0ustar lkundraklkundrakuse strict; use warnings; use Module::Build; my $libsystemd_journal_CFLAGS = `pkg-config --cflags libsystemd-journal` or warn 'Could not determine systemd-journal compiler flags'; my $libsystemd_journal_LDFLAGS = `pkg-config --libs libsystemd-journal` or warn 'Could not determine systemd-journal compiler flags'; my $build = Module::Build->new( module_name => 'Log::Journald', license => 'perl', dist_author => 'Lubomir Rintel ', create_readme => 1, meta_merge => { resources => { bugtracker => 'http://rt.cpan.org/NoAuth/Bugs.html?Dist=Log-Journald', repository => 'https://github.com/lkundrak/perl-Log-Journald', } }, extra_compiler_flags => $libsystemd_journal_CFLAGS, extra_linker_flags => $libsystemd_journal_LDFLAGS, configure_requires => { 'Module::Build' => 0, }, build_requires => { 'ExtUtils::CBuilder' => 0, } ); $build->create_build_script; Log-Journald-0.30/META.json0000444001762600176260000000245212402074174015606 0ustar lkundraklkundrak{ "abstract" : "Send messages to a systemd journal", "author" : [ "Lubomir Rintel " ], "dynamic_config" : 1, "generated_by" : "Module::Build version 0.4007, CPAN::Meta::Converter version 2.132140", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Log-Journald", "prereqs" : { "build" : { "requires" : { "ExtUtils::CBuilder" : "0" } }, "configure" : { "requires" : { "Module::Build" : "0" } } }, "provides" : { "Log::Dispatch::Journald" : { "file" : "lib/Log/Dispatch/Journald.pm" }, "Log::Journald" : { "file" : "lib/Log/Journald.pm", "version" : "0.30" }, "Log::Log4perl::Appender::Journald" : { "file" : "lib/Log/Log4perl/Appender/Journald.pm" } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "http://rt.cpan.org/NoAuth/Bugs.html?Dist=Log-Journald" }, "license" : [ "http://dev.perl.org/licenses/" ], "repository" : { "url" : "https://github.com/lkundrak/perl-Log-Journald" } }, "version" : "0.30" } Log-Journald-0.30/META.yml0000444001762600176260000000146512402074174015441 0ustar lkundraklkundrak--- abstract: 'Send messages to a systemd journal' author: - 'Lubomir Rintel ' build_requires: ExtUtils::CBuilder: 0 configure_requires: Module::Build: 0 dynamic_config: 1 generated_by: 'Module::Build version 0.4007, CPAN::Meta::Converter version 2.132140' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: Log-Journald provides: Log::Dispatch::Journald: file: lib/Log/Dispatch/Journald.pm Log::Journald: file: lib/Log/Journald.pm version: 0.30 Log::Log4perl::Appender::Journald: file: lib/Log/Log4perl/Appender/Journald.pm resources: bugtracker: http://rt.cpan.org/NoAuth/Bugs.html?Dist=Log-Journald license: http://dev.perl.org/licenses/ repository: https://github.com/lkundrak/perl-Log-Journald version: 0.30 Log-Journald-0.30/MANIFEST0000444001762600176260000000046212402074174015315 0ustar lkundraklkundrakBuild.PL Build scipt MANIFEST This file t/Log-Journald.t A test lib/Log/Journald.pm Perl wrapper and documentation lib/Log/Journald.xs Bindings to C language library lib/Log/Dispatch/Journald.pm Log::Dispatch backend lib/Log/Log4perl/Appender/Journald.pm Log::Log4perl appender META.yml META.json README Log-Journald-0.30/README0000444001762600176260000000574312402074174015053 0ustar lkundraklkundrakNAME Log::Journald - Send messages to a systemd journal SYNOPSIS use Log::Journald; use Sys::Syslog qw/:macros/; # Easy. journal_log(LOG_INFO, "Hello (1) from PID $$!"); # Send arbitrary fields, even binary data Log::Journald::send(PRIORITY => LOG_INFO, MESSAGE => "Hello (2) from PID $$!", PERL_PACKAGE => __PACKAGE__, _YOLO => "SW\x00AG") or warn "Could not send log: $!"; # Raw Log::Journald::sendv('PRIORITY=6', "MESSAGE=Hello (3) from PID $$!"); Please consider this an alpha quality code, whose API can change at any time, until we reach version 1.00. DESCRIPTION This module wraps sd-journal(3) APIs for easy use in Perl. It makes it possible to easily use systemd-journald.service(8)'s structured logging capabilities and includes location of the logging point in the source code in the messages. Backends for Log::Dispatch and Log::Log4perl exist: Use Log::Dispatch::Journald and Log::Log4perl::Appender::Journald respectively. SUBROUTINES journal_log PRIORITY MESSAGE Log a message at given priority. Exported by default. Returns true upon success, false while setting $! on failure. send KEY VALUE ... Log a message with given key-value pairs. "MESSAGE" and "PRIORITY" keys are mandatory. See systemd.journal-fields(7) for list and description of known fields. Returns true upon success, false while setting $! on failure. sendv STRING ... Same as above, apart from that instead of key and value pair, strings that contain key and value concatenated with "=" are expected. This avoids an extra copy and might me slightly more efficient. Returns true upon success, false while setting $! on failure. SEE ALSO * Sys::Syslog -- Traditiona logging mechanism. The module provides useful macros. * sd-journal(3) -- Description of C language API for journal. * systemd-journald.service(8) -- Manual of the journal service. * Log::Dispatch::Journald -- Log::Dispatch backend. * Log::Log4perl::Appender::Journald -- Log::Log4perl appender. BUGS "journal_log()" terminates the message at a NUL byte. You need to use another interface to log binary data. To get priority constants, you still need to include Sys::Syslog. There's no way to override caller depth. Therefore if you add a wrapper for any of this module's interfaces, you'll get the location of the wrapper in the messages. A way to disable or override inclusion of code location would be nice. COPYRIGHT Copyright 2014 Lubomir Rintel This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. AUTHORS Lubomir Rintel, The code is hosted on GitHub . Bug fixes and feature enhancements are always welcome. Log-Journald-0.30/lib/0000775001762600176260000000000012402074174014734 5ustar lkundraklkundrakLog-Journald-0.30/lib/Log/0000775001762600176260000000000012402074174015455 5ustar lkundraklkundrakLog-Journald-0.30/lib/Log/Journald.pm0000444001762600176260000000630212402074174017566 0ustar lkundraklkundrak=head1 NAME Log::Journald - Send messages to a systemd journal =head1 SYNOPSIS use Log::Journald; use Sys::Syslog qw/:macros/; # Easy. journal_log(LOG_INFO, "Hello (1) from PID $$!"); # Send arbitrary fields, even binary data Log::Journald::send(PRIORITY => LOG_INFO, MESSAGE => "Hello (2) from PID $$!", PERL_PACKAGE => __PACKAGE__, _YOLO => "SW\x00AG") or warn "Could not send log: $!"; # Raw Log::Journald::sendv('PRIORITY=6', "MESSAGE=Hello (3) from PID $$!"); Please consider this an alpha quality code, whose API can change at any time, until we reach version 1.00. =head1 DESCRIPTION This module wraps L APIs for easy use in Perl. It makes it possible to easily use L's structured logging capabilities and includes location of the logging point in the source code in the messages. Backends for L and L exist: Use L and L respectively. =cut package Log::Journald; use strict; use warnings; require Exporter; our $VERSION = '0.30'; our @ISA = qw/Exporter/; our @EXPORT_OK = qw/journal_log send sendv/; our @EXPORT = qw/journal_log send sendv/; require XSLoader; XSLoader::load('Log::Journald', $VERSION); 1; =head1 SUBROUTINES =over 4 =item B PRIORITY MESSAGE Log a message at given priority. Exported by default. Returns true upon success, false while setting C<$!> on failure. =item B KEY VALUE ... Log a message with given key-value pairs. C and C keys are mandatory. See L for list and description of known fields. Returns true upon success, false while setting C<$!> on failure. =item B STRING ... Same as above, apart from that instead of key and value pair, strings that contain key and value concatenated with "=" are expected. This avoids an extra copy and might me slightly more efficient. Returns true upon success, false while setting C<$!> on failure. =back =head1 SEE ALSO =over =item * L -- Traditiona logging mechanism. The module provides useful macros. =item * L -- Description of C language API for journal. =item * L -- Manual of the journal service. =item * L -- L backend. =item * L -- L appender. =back =head1 BUGS C terminates the message at a NUL byte. You need to use another interface to log binary data. To get priority constants, you still need to include L. There's no way to override caller depth. Therefore if you add a wrapper for any of this module's interfaces, you'll get the location of the wrapper in the messages. A way to disable or override inclusion of code location would be nice. =head1 COPYRIGHT Copyright 2014 Lubomir Rintel This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 AUTHORS Lubomir Rintel, L<< >> The code is hosted on GitHub L. Bug fixes and feature enhancements are always welcome. Log-Journald-0.30/lib/Log/Journald.xs0000444001762600176260000000670512402074174017613 0ustar lkundraklkundrak#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include /* We determine our location from the Perl code instead. */ #define SD_JOURNAL_SUPPRESS_LOCATION #include /* Extra fields to be added at the end of each log vector. */ enum { CODE_FILE, CODE_LINE, EXTRA_CNT }; /* Fill in an iovec entry with a formatted string. */ #define IOVEC_FMT(vec, ...) do { \ (vec).iov_len = asprintf ((char **)&(vec).iov_base, __VA_ARGS__); \ if ((vec).iov_len == -1) \ (vec).iov_base = NULL; \ } while(0) /* Allocate iovec for given number of fields, automatically filling extra * entries at the end. */ static struct iovec * alloc_iovec (items) const int items; { struct iovec *logv; logv = malloc(sizeof (struct iovec) * (items + EXTRA_CNT)); if (logv == NULL) return NULL; IOVEC_FMT(logv[items + CODE_FILE], "CODE_FILE=%s", OutCopFILE(PL_curcop)); IOVEC_FMT(logv[items + CODE_LINE], "CODE_LINE=%d", CopLINE(PL_curcop)); return logv; } /* Send the vector to the journal and deallocate the extra entries we've * allcoated in alloc_iovec(). Handle errors in the manner more typical to * Perl (return -1 & set errno as opposed to returning -errno. */ static int submit_iovec (logv, items) struct iovec *logv; const int items; { int err; err = sd_journal_sendv(logv, items + EXTRA_CNT); free(logv[items + CODE_FILE].iov_base); free(logv[items + CODE_LINE].iov_base); if (err) { errno = -err; return -1; } return 0; } MODULE = Log::Journald PACKAGE = Log::Journald # A simple print function. Just logs in a single message with a priority. int journal_log(prio, msg) int prio; const char *msg; CODE: struct iovec *logv; logv = alloc_iovec(2); if (logv == NULL) { errno = ENOMEM; XSRETURN_UNDEF; } IOVEC_FMT(logv[0], "PRIORITY=%d", prio); IOVEC_FMT(logv[1], "MESSAGE=%s", msg); if (submit_iovec(logv, items) == 0) { RETVAL = 1; } else { RETVAL = 0; } free(logv[0].iov_base); free(logv[1].iov_base); free(logv); if (RETVAL == 0) XSRETURN_UNDEF; OUTPUT: RETVAL # Raw log sending function. Sends its arguments as they are. int send(key, value, ...) CODE: int i; struct iovec *logv; if (items % 2) croak("odd arguments to Log::Journald::send"); logv = alloc_iovec(items / 2); if (logv == NULL) { errno = ENOMEM; XSRETURN_UNDEF; } for (i = 0; i < items; i += 2) { STRLEN key_len, val_len, len; const char *key, *val; char *msg; key = SvPV(ST(i), key_len); val = SvPV(ST(i+1), val_len); len = key_len + 1 + val_len; logv[i / 2].iov_base = msg = malloc(len); if (msg == NULL) { logv[i / 2].iov_len = -1; } else { memcpy(msg, key, key_len); msg += key_len; *msg++ = '='; memcpy(msg, val, val_len); logv[i / 2].iov_len = len; } } if (submit_iovec(logv, items / 2) == 0) { RETVAL = 1; } else { RETVAL = 0; } for (i = 0; i < items; i += 2) free (logv[i / 2].iov_base); free(logv); if (RETVAL == 0) XSRETURN_UNDEF; OUTPUT: RETVAL # Raw log sending function. Sends its arguments as they are. int sendv(arg, ...) CODE: int i; struct iovec *logv; logv = alloc_iovec(items); if (logv == NULL) { errno = ENOMEM; XSRETURN_UNDEF; } for (i = 0; i < items; i++) { logv[i].iov_base = SvPV(ST(i), logv[i].iov_len); } if (submit_iovec(logv, items) == 0) { RETVAL = 1; } else { RETVAL = 0; } free(logv); if (RETVAL == 0) XSRETURN_UNDEF; OUTPUT: RETVAL Log-Journald-0.30/lib/Log/Dispatch/0000775001762600176260000000000012402074174017214 5ustar lkundraklkundrakLog-Journald-0.30/lib/Log/Dispatch/Journald.pm0000444001762600176260000000330412402074174021324 0ustar lkundraklkundrak=head1 NAME Log::Dispatch::Journald - Journald log dispatcher =head1 SYNOPSIS use Log::Dispatch; my $log = Log::Dispatch->new( outputs => [['Journald', min_level => 'info', ident => 'Wheee' ]]); $log->info('Time to die.'); $log->log(level => 'error', message => 'Time to die.', yolo => 'Swag'); =head1 DESCRIPTION This module provides a L backend for L. It is possible to log arbitrary key-value pairs using the Journald's structured logging capability. =cut package Log::Dispatch::Journald; use strict; use warnings; use Log::Dispatch::Output; use base qw( Log::Dispatch::Output ); use Params::Validate qw(validate ARRAYREF SCALAR); Params::Validate::validation_options( allow_extra => 1 ); use Log::Journald; sub new { my $proto = shift; my $class = ref $proto || $proto; my %p = @_; my $self = bless {}, $class; $self->_basic_init(%p); return $self; } sub log_message { my $self = shift; my %p = @_; my %log; $log{PRIORITY} = $self->_level_as_number( $p{level} ); while (my ($key, $value) = each %p) { $log{uc $key} = $value; } Log::Journald::send (%log) or warn $!; } 1; =head1 SEE ALSO =over =item * L -- Log dispatcher =item * L -- Use this one with L =back =head1 COPYRIGHT Copyright 2014 Lubomir Rintel This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 AUTHORS Lubomir Rintel, L<< >> The code is hosted on GitHub L. Bug fixes and feature enhancements are always welcome. Log-Journald-0.30/lib/Log/Log4perl/0000775001762600176260000000000012402074174017145 5ustar lkundraklkundrakLog-Journald-0.30/lib/Log/Log4perl/Appender/0000775001762600176260000000000012402074174020703 5ustar lkundraklkundrakLog-Journald-0.30/lib/Log/Log4perl/Appender/Journald.pm0000444001762600176260000000352312402074174023016 0ustar lkundraklkundrak=head1 NAME Log::Log4perl::Appender::Journald - Journald appender for Log4perl =head1 SYNOPSIS use Log::Log4perl; my $log4perl_conf = <init(\$log4perl_conf); Log::Log4perl::MDC->put(HELLO => 'World'); my $logger = Log::Log4perl->get_logger('log4perl.rootLogger'); $logger->info("Time to die."); $logger->error("Time to err."); =head1 DESCRIPTION This module provides a L appender that directs log messages to L via L. It makes use of the structured logging capability, appending Log4perl MDCs with each message. =cut package Log::Log4perl::Appender::Journald; our @ISA = qw/Log::Log4perl::Appender/; use warnings; use strict; use Log::Journald; use Log::Log4perl::MDC; sub new { bless {}, shift; } sub log { my $self = shift; my %params = @_; my $mdc = Log::Log4perl::MDC->get_context(); my %log; while (my ($key, $value) = each %params) { $log{uc $key} = $value; } # Add MDCs while (my ($key, $value) = each %$mdc) { $log{uc $key} = $value; } # Turn syslog level into journald priority $log{PRIORITY} = 7 - delete $log{LEVEL}; Log::Journald::send (%log) or warn $!; } 1; =head1 SEE ALSO =over =item * L -- Journal daemon client bindings. =item * L -- A logging framework =back =head1 COPYRIGHT Copyright 2014 Lubomir Rintel This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 AUTHORS Lubomir Rintel, L<< >> The code is hosted on GitHub L. Bug fixes and feature enhancements are always welcome. Log-Journald-0.30/t/0000775001762600176260000000000012402074174014431 5ustar lkundraklkundrakLog-Journald-0.30/t/Log-Journald.t0000444001762600176260000000022712402074174017110 0ustar lkundraklkundrak# Basically just tests this imports fine. Needs improvement. use strict; use warnings; use Test::More tests => 1; BEGIN { use_ok('Log::Journald') };