Skip to content

Logging #55

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

Open
wants to merge 7 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
4 changes: 4 additions & 0 deletions bin/exobrain
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ elsif ($command eq 'setup') {

my $exe = $0;
my $service = "$ubic/service/";

use Exobrain::Logger;
Exobrain::Logger->new->setup;

say "Copying $exe to $service";
copy($0, $service);
Expand Down Expand Up @@ -224,6 +227,7 @@ sub ubic_services {

core => Ubic::Multiservice::Simple->new({
router => daemon("router"),
logging => daemon("logging"),
}),
});
}
24 changes: 24 additions & 0 deletions bin/logging
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/perl -w
use 5.010;
use strict;
use warnings;
use autodie;

# PODNAME: logging
# ABSTRACT: Exobrain logging. Provides informational or debug logging of the exobrain bus
# VERSION

use Exobrain;

my $exobrain = Exobrain->new;
$exobrain->log->info("Starting Exobrain Bus Logger");

while (1) {
if ($exobrain->log->is_debug()) {
$exobrain->log->debug($exobrain->sub->get->dump);
}
else {
$exobrain->log->info($exobrain->sub->get->summary);
}
}

9 changes: 9 additions & 0 deletions lib/Exobrain.pm
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ The following are methods provided by the top-level Exobrain object:
# lazy attributes, and nice sugar for doing common operations.

use Exobrain::Bus;
use Exobrain::Logger;
use Exobrain::Config;
use Exobrain::Message;
use Exobrain::Message::Raw;
Expand All @@ -82,6 +83,12 @@ has 'config' => (
builder => '_build_config',
);

has 'log' => (
is => 'ro',
isa => 'Log::Log4perl::Logger',
builder => '_build_logger',
);

# Pub/Sub interfaces to our bus. These don't get generated unless
# our end code actually asks for them. Many things will only require
# one, or will use higher-level functions to do their work.
Expand Down Expand Up @@ -148,6 +155,8 @@ method _set_timezone($tz?) {
return;
}

sub _build_logger { my $log = Exobrain::Logger->new;
return $log->log; };
sub _build_config { return Exobrain::Config->new; };
sub _build_pub { return Exobrain::Bus->new(type => 'PUB', exobrain => shift) }
sub _build_sub { return Exobrain::Bus->new(type => 'SUB', exobrain => shift) }
Expand Down
78 changes: 78 additions & 0 deletions lib/Exobrain/Logger.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package Exobrain::Logger;
use v5.10.0;
use Log::Log4perl;

use Moose;
use Method::Signatures;
use Exobrain::Config;

# ABSTRACT: Simple Log4Perl access
# VERSION

=head1 DESCRIPTION

Inspired by MooseX::Log::Log4perl and provides a thin wrapper to Log::Log4perl.

Simply using the module will automatically log to the Exobrain log. Via Exobrain:

use Exobrain;

my $exobrain->new;
$exobrain->log->info("Such logging!");

or inside your own exobrain component

use Exobrain::Logger;

my $logger = Exobrain::Logger->new;
$logger->log->info("Very log!");

All standard Log4perl methods are available.

To see more verbose output, change Logger.ini level to DEBUG.

=cut

has log => (
is => 'ro',
isa => 'Log::Log4perl::Logger',
lazy => 1,
builder => '_build_log',
);

method _build_log() {
my $config = Exobrain::Config->new;

# Tests will fail if no config exists yet.
if (! $config->{Logger}{level}) {
$config->{Logger}{level} = 'INFO';
}
if (! $config->{Logger}{file}) {
$config->{Logger}{file} = '/tmp/exobrain.log';
}

# In reality this whole lot could be loaded from a log.conf, but the above may be simpler.
my $log_conf = qq(
log4perl.rootLogger = $config->{Logger}{level}, Exobrain
log4perl.appender.Exobrain = Log::Log4perl::Appender::File
log4perl.appender.Exobrain.utf8 = 1
log4perl.appender.Exobrain.filename = $config->{Logger}{file}
log4perl.appender.Exobrain.mode = append
log4perl.appender.Exobrain.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.Exobrain.layout.ConversionPattern = %d %p %m %n
);

Log::Log4perl->init_once(\$log_conf);
return Log::Log4perl->get_logger();
}

method setup() {
my $config =
"[Logger]\n" .
"file = $ENV{HOME}/ubic/log/exobrain/exobrain.log\n" .
"level = INFO\n";

my $filename = Exobrain::Config->write_config('Logger.ini', $config);
}

1;