AnyEvent

 view release on metacpan or  search on metacpan

lib/AnyEvent/Log.pm  view on Meta::CPAN

the raw message string and needs to return a formatted log message. In
most cases this will be a string, but it could just as well be an array
reference that just stores the values.

If, for some reason, you want to use C<caller> to find out more about the
logger then you should walk up the call stack until you are no longer
inside the C<AnyEvent::Log> package.

To implement your own logging callback, you might find the
C<AnyEvent::Log::format_time> and C<AnyEvent::Log::default_format>
functions useful.

Example: format the message just as AnyEvent::Log would, by letting
AnyEvent::Log do the work. This is a good basis to design a formatting
callback that only changes minor aspects of the formatting.

   $ctx->fmt_cb (sub {
      my ($time, $ctx, $lvl, $msg) = @_;

      AnyEvent::Log::default_format $time, $ctx, $lvl, $msg
   });

Example: format just the raw message, with numeric log level in angle
brackets.

   $ctx->fmt_cb (sub {
      my ($time, $ctx, $lvl, $msg) = @_;

      "<$lvl>$msg\n"
   });

Example: return an array reference with just the log values, and use
C<PApp::SQL::sql_exec> to store the message in a database.

   $ctx->fmt_cb (sub { \@_ });
   $ctx->log_cb (sub {
      my ($msg) = @_;

      sql_exec "insert into log (when, subsys, prio, msg) values (?, ?, ?, ?)",
               $msg->[0] + 0,
               "$msg->[1]",
               $msg->[2] + 0,
               "$msg->[3]";

      0
   });

=item $ctx->log_to_warn

Sets the C<log_cb> to simply use C<CORE::warn> to report any messages
(usually this logs to STDERR).

=item $ctx->log_to_file ($path)

Sets the C<log_cb> to log to a file (by appending), unbuffered. The
function might return before the log file has been opened or created.

=item $ctx->log_to_path ($path)

Same as C<< ->log_to_file >>, but opens the file for each message. This
is much slower, but allows you to change/move/rename/delete the file at
basically any time.

Needless(?) to say, if you do not want to be bitten by some evil person
calling C<chdir>, the path should be absolute. Doesn't help with
C<chroot>, but hey...

=item $ctx->log_to_syslog ([$facility])

Logs all messages via L<Sys::Syslog>, mapping C<trace> to C<debug> and
all the others in the obvious way. If specified, then the C<$facility> is
used as the facility (C<user>, C<auth>, C<local0> and so on). The default
facility is C<user>.

Note that this function also sets a C<fmt_cb> - the logging part requires
an array reference with [$level, $str] as input.

=cut

sub log_cb {
   my ($ctx, $cb) = @_;

   $ctx->[3] = $cb;
}

sub fmt_cb {
   my ($ctx, $cb) = @_;

   $ctx->[4] = $cb;
}

sub log_to_warn {
   my ($ctx, $path) = @_;

   $ctx->log_cb (sub {
      warn shift;
      0
   });
}

# this function is a good example of why threads are a must,
# simply for priority inversion.
sub _log_to_disk {
   # eval'uating this at runtime saves 220kb rss - perl has become
   # an insane memory waster.
   eval q{ # poor man's autoloading {}
      sub _log_to_disk {
         my ($ctx, $path, $keepopen) = @_;

         my $fh;
         my @queue;
         my $delay;
         my $disable;

         use AnyEvent::IO ();

         my $kick = sub {
            undef $delay;
            return unless @queue;
            $delay = 1;



( run in 3.716 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )