Log::Trivial - Very simple tool for writing very simple log files


Log-Trivial documentation  | view source Contained in the Log-Trivial distribution.

Index


NAME

Top

Log::Trivial - Very simple tool for writing very simple log files

SYNOPSIS

Top

  use Log::Trivial;
  my $logger = Log::Trivial->new(log_file => "path/to/my/file.log");
  $logger->set_level(3);
  $logger->write(comment => "foo");

DESCRIPTION

Top

Use this module when you want use "Yet Another" very simple, light weight log file writer.

SUBROUTINES/METHODS

Top

new

The constructor can be called empty or with a number of optional parameters.

  $logger = Log::Trivial->new();

or

  $logger = Log::Trivial->new(
    log_file  => "/my/config/file",
    log_tag   => $$,
    log_level => "2");

The log_tag is an optional string that is written to every log event between the date at the comment, and is intended to help separate logging events in environments when multiple applictions are simultaneously writing to the same log file. For example you could pass the PID of the applications, as shown in the example above.

set_log_file

The log file can be set after the constructor has been called. Simply set the path to the file you want to use as the log file.

  $logger->set_log_file("/path/to/log.file");

set_log_mode

Log::Trivial runs in two modes. The default mode is Multi mode: in this mode the file will be opened and closed for each log file write. This may be slower but allows multiple applications to write to the log file at the same time. The alternative mode is called single mode: once you start to write to the log no other application honouring flock will write to the log. Single mode is potentially faster, and may be appropriate if you know that only one copy of your application can should be writing to the log at any given point in time.

WARNING: Not all system honour flock.

  $logger->set_log_mode("multi");    # Sets multi mode (the default)

or

  $logger->set_log_mode("single");    # Sets single mode

set_log_level

Log::Trivial uses very simple arbitrary logging level logic. Level 0 is the highest priority log level, the lowest is the largest number possible in Perl on your platform. You set the global log level for your application using this function, and only log events of this level or higher priority will be logged. The default level is 3.

  $logger->set_log_level(4);

set_write_mode

Log::Trivial write log enteries using the POSIX synchronous mode by default. This mode ensures that the data has actually been written to the disk. This feature is not supported in all operating systems and will slow down the disk write. By default this mode is enabled, in future it may be disabled by default.

  $logger->set_write_mode('s');     # sets synchronous (default)
  $logger->set_write_mode('a');     # sets asynchronous

write

Write a log file entry.

  $logger->write(
    comment => "My comment to be logged",
    level   => 3);

or

  $logger->write("My comment to be logged");

It will fail if the log file hasn't be defined, or isn't writable. It will return the string written on success.

If you don't specify a log level, it will default to the current log level and therefore log the event. Therefore if you always wish to log something either specify a level of 0 or never specify a log level.

Log file entries are time stamped and have a newline carriage return added automatically.

get_error

In normal operation the module should never die. All errors are non-fatal. If an error occurs it will be stored internally within the object and the method will return undef. The error can be read with the get_error method. Only the most recent error is stored.

  $logger->write("Log this") || print $logger->get_error;

LOG FORMAT

Top

The log file format is very simple and fixed:

Time & date [tab] Your log comment [carriage return new line]

If you have enabled a log_tag then the log format will have an extra element inserted in it.

Time & date [tab] log_tag [tab] Your log comment [carriage return new line]

DEPENDENCIES

At the moment the module only uses core modules. The test suite optionally uses Pod::Coverage, Test::Pod::Coverage and Test::Pod, which will be skipped if you don't have them.

History

See Changes file.

BUGS AND LIMITATIONS

Top

By default log write are POSIX synchronous, it is very unlikely that it will run on any OS that does not support POSIX synchronous file writing, this means it probably won't run on a VAX, Windows or other antique system. It does run under Windows/Cygwin. To use non-POSIX systems you need to turn off synchronous write.

Patches Welcome... ;-)

To Do

EXPORT

Top

None.

AUTHOR

Top

Adam Trickett, <atrickett@cpan.org>

SEE ALSO

Top

perl, Log::Agent, Log::Log4perl, Log::Dispatch, Log::Simple

LICENSE AND COPYRIGHT

Top


Log-Trivial documentation  | view source Contained in the Log-Trivial distribution.