[Catalyst] Recommendation For Logging To A File
Gavin Henry
ghenry at perl.me.uk
Sun Sep 24 22:55:37 CEST 2006
<quote who="hkclark at gmail.com">
> What is the "recommended best way" (or the 2-3 top contenders if there
> is no "one best way") to have a Catalyst app log in production to a
> file (at least I assume that's what most people want their production
> apps to do). Is Catalyst::Log::Log4perl the way to go?
Well, for our site (http://www.suretecsystems.com), we use Log4perl, but
it hosted in a weird kind of way by our friend, so we don't get access to
the Apache logs directly, like Bill does. We therefore trap stderr etc.
and log to our own file like so (probably hits our performance, but hey):
Suretec.pm (with rest of stuff snipped out, see Log4perl docs)
use Suretec::Log::Trapper;
# Setup our Log4perl first, to capture all startup messages and Redirect
# STDERR/STDOUT for no Apache logs
tie *STDERR, 'Suretec::Log::Trapper';
tie *STDOUT, 'Suretec::Log::Trapper';
__PACKAGE__->log( Catalyst::Log::Log4perl->new(
__PACKAGE__->path_to('Log4perl.conf')->stringify ) );
Our Log4perl.conf
############################################################
#
# Suretec Systems Ltd. - 03/07/2006
#
# A simple root logger with a Log::Log4perl::Appender::File
# file appender in Perl.
############################################################
log4perl.rootLogger=ALL, LOGFILE
log4perl.appender.LOGFILE=Log::Log4perl::Appender::File
log4perl.appender.LOGFILE.filename= \
sub { return Suretec->path_to('log/suretec.log'); }
log4perl.appender.LOGFILE.mode=append
log4perl.appender.LOGFILE.layout=PatternLayout
log4perl.appender.LOGFILE.layout.ConversionPattern=%d %F %L %c - %m%n
--------------------
package Suretec::Log::Trapper;
#===============================================================================
#
# FILE: Trapper.pm
#
# DESCRIPTION: Redirect STDERR etc. to Log4perl
#
# FILES: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Gavin Henry (GH), <ghenry at suretecsystems.com>
# COMPANY: Suretec Systems Ltd.
# VERSION: 1.0
# CREATED: 07/03/2006 11:19:40 AM BST
# REVISION: ---
#===============================================================================
our $VERSION = '1.0';
use strict;
use warnings;
use Log::Log4perl qw(:easy);
sub TIEHANDLE {
my $class = shift;
bless [], $class;
}
sub PRINT {
my $self = shift;
$Log::Log4perl::caller_depth++;
DEBUG @_;
$Log::Log4perl::caller_depth--;
}
sub CLOSE {
my $self = shift;
$Log::Log4perl::caller_depth++;
DEBUG @_;
$Log::Log4perl::caller_depth--;
}
sub OPEN {
my $self = shift;
$Log::Log4perl::caller_depth++;
DEBUG @_;
$Log::Log4perl::caller_depth--;
}
More information about the Catalyst
mailing list