The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Acrux::Log - Acrux logger

SYNOPSIS

use Acrux::Log;

# Using syslog
my $log = Acrux::Log->new();
   $log->error("My test error message to syslog")

# Using file
my $log = Acrux::Log->new(file => '/tmp/test.log');
   $log->error("My test error message to /tmp/test.log")

# Using STDOUT (handle)
my $log = Acrux::Log->new(
        handle => IO::Handle->new_from_fd(fileno(STDOUT), "w")
    );
$log->error("My test error message to STDOUT")

# Customize minimum log level
my $log = Acrux::Log->new(level => 'warn');

# Log messages
$log->trace('Doing stuff');
$log->debug('Not sure what is happening here');
$log->info('FYI: it happened again');
$log->notice('Normal, but significant, condition...');
$log->warn('This might be a problem');
$log->error('Garden variety error');
$log->fatal('Boom');
$log->crit('Its over...');
$log->alert('Action must be taken immediately');
$log->emerg('System is unusable');

DESCRIPTION

Acrux::Log is a simple logger for Acrux logging

new

my $log = Acrux::Log->new(
    logopt      => 'ndelay,pid',
    facility    => 'user',
    level       => 'debug',
    ident       => 'test.pl',
    autoclean   => 1,
    logopt      => 'ndelay,pid',
);

With default attributes

use Mojo::Log;
my $log = Acrux::Log->new( logger => Mojo::Log->new );
$log->error("Test error message");

This is example with external loggers

ATTRIBUTES

This class implements the following attributes

autoclean

autoclean => 1

This attribute enables cleaning (closing handler or syslog) on DESTROY

color

color => 1

Colorize log messages with the available levels using Term::ANSIColor, defaults to 0

facility

facility => 'user'

This attribute sets facility for logging

Available standard facilities: auth, authpriv, cron, daemon, ftp, kern, local0, local1, local2, local3, local4, local5, local6, local7, lpr, mail, news, syslog, user and uucp

Default: user (Sys::Syslog::LOG_USER)

See also "Facilities" in Sys::Syslog

file

file => '/var/log/myapp.log'

Log file path used by "handle"

format

format => sub {...}

A callback function for formatting log messages

format => sub {
    my ($time, $level, @lines) = @_;
    return "[$time] [$level] " . join (' ', @lines) . "\n";
}

This callback routine must return formatted string for the log line

handle

handle => IO::Handle->new_from_fd(fileno(STDOUT), "w")

Log filehandle, defaults to opening "file" or uses syslog if file not specified

ident

ident => 'myapp'

The ident is prepended to every syslog message

Default: script name basename($0)

level

level => 'debug'

There are six predefined log levels: fatal, error, warn, info, debug, and trace (in descending priority). The syslog supports followed additional log levels: emerg, alert, crit' and notice (in descending priority). But we recommend not using them to maintain compatibility. Your configured logging level has to at least match the priority of the logging message.

If your configured logging level is warn, then messages logged with info(), debug(), and trace() will be suppressed; fatal(), error() and warn() will make their way through, because their priority is higher or equal than the configured setting.

Default: debug

See also "Levels" in Sys::Syslog

logger

logger => Mojo::Log->new()

This attribute perfoms to set predefined logger, eg. Mojo::Log

Default: undef

logopt

logopt => 'ndelay,pid'

This attribute contains zero or more of the options detailed in "openlog" in Sys::Syslog

Default: 'ndelay,pid'

prefix

prefix => '>>>'

The prefix is prepended to every handled log message

Default: null

short

short => 1

Generate short log messages without a timestamp but with log level prefix, defaults to 0

METHODS

This class implements the following methods

alert

$log->alert('Action must be taken immediately');
$log->alert('Real', 'problem');

Log alert message

crit

$log->crit('Its over...');
$log->crit('Bye', 'bye');

Log crit message (See "fatal" method)

debug

$log->debug('You screwed up, but that is ok');
$log->debug('All', 'cool');

Log debug message

emerg

$log->emerg('System is unusable');
$log->emerg('To', 'die');

Log emerg message

error

$log->error('You really screwed up this time');
$log->error('Wow', 'seriously');

Log error message

fatal

$log->fatal('Its over...');
$log->fatal('Bye', 'bye');

Log fatal message

info

$log->info('You are bad, but you prolly know already');
$log->info('Ok', 'then');

Log info message

level

my $level = $log->level;
$log      = $log->level('debug');

Active log level, defaults to debug. Available log levels are trace, debug, info, notice, warn, error, fatal (crit), alert and emerg, in that order

logger

my $logger = $log->logger;

This method returns the logger object or undef if not exists

notice

$log->notice('Normal, but significant, condition...');
$log->notice('Ok', 'then');

Log notice message

provider

print $log->provider;

Returns provider name (external, handle, file or syslog)

trace

$log->trace('Whatever');
$log->trace('Who', 'cares');

Log trace message

warn

$log->warn('Dont do that Dave...');
$log->warn('No', 'really');

Log warn message

HISTORY

See Changes file

TO DO

See TODO file

SEE ALSO

Sys::Syslog

AUTHOR

Serż Minus (Sergey Lepenkov) https://www.serzik.com <abalama@cpan.org>

COPYRIGHT

Copyright (C) 1998-2024 D&D Corporation. All Rights Reserved

LICENSE

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See LICENSE file and https://dev.perl.org/licenses/