| 1 | package File::SmartTail::Logger; | ||
| 2 | # | ||
| 3 | # $Id: Log4perlLogger.pm,v 1.6 2008/07/09 20:40:20 mprewitt Exp $ | ||
| 4 | # | ||
| 5 | # This file or one of the other loggers is copied to File/SmartTail/Logger.pm | ||
| 6 | # during the 'perl Makefile.PL' process. Do not edit File/SmartTail/Logger.pm | ||
| 7 | # directly. Edit one of the other Loggers and run make. | ||
| 8 | # | ||
| 9 | # DMJA, Inc <smarttail@dmja.com> | ||
| 10 | # | ||
| 11 | # Copyright (C) 2003-2008 DMJA, Inc, File::SmartTail comes with | ||
| 12 | # ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to | ||
| 13 | # redistribute it and/or modify it under the same terms as Perl itself. | ||
| 14 | # See the "The Artistic License" L<LICENSE> for more details. | ||
| 15 | |||
| 16 | { | ||
| 17 | my $v; | ||
| 18 | sub LOG { | ||
| 19 | $v and return $v; | ||
| 20 | |||
| 21 | _init_log(); | ||
| 22 | |||
| 23 | $v = Log::Log4perl->get_logger(__PACKAGE__); | ||
| 24 | $v->debug("Initializing " . __PACKAGE__ ); | ||
| 25 | |||
| 26 | return $v; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | use constant LOGFILE => 'log4perl.conf'; | ||
| 31 | |||
| 32 | { | ||
| 33 | my $init_log; | ||
| 34 | sub _init_log { | ||
| 35 | my $logpath = shift || join '/', _log4perl_conf_dir(), LOGFILE; | ||
| 36 | |||
| 37 | require Log::Log4perl; | ||
| 38 | require Log::Dispatch::Screen; | ||
| 39 | require Log::Log4perl::Appender; | ||
| 40 | |||
| 41 | if (-f $logpath) { | ||
| 42 | # print STDERR "Using $logpath\n"; | ||
| 43 | Log::Log4perl->init_and_watch($logpath, 30); | ||
| 44 | } else { | ||
| 45 | # print STDERR "No @{[ LOGFILE() ]} file found, creating default\n"; | ||
| 46 | my $app = Log::Log4perl::Appender->new("Log::Dispatch::Screen"); | ||
| 47 | my $layout = Log::Log4perl::Layout::PatternLayout->new("%d %p %m %n"); | ||
| 48 | $app->layout($layout); | ||
| 49 | my $logger = Log::Log4perl::Logger->get_root_logger(); | ||
| 50 | $logger->level($Log::Log4perl::FATAL); | ||
| 51 | # | ||
| 52 | # Only do this once so we don't get duplicate appenders | ||
| 53 | # | ||
| 54 | $logger->add_appender($app) unless $init_log; | ||
| 55 | $init_log = 1; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _log4perl_conf_dir { | ||
| 61 | require Cwd; | ||
| 62 | my $cwd = Cwd::getcwd(); | ||
| 63 | my $logfile = LOGFILE; | ||
| 64 | while ( $cwd && $cwd ne "/" && !-f "$cwd/$logfile") { | ||
| 65 | $cwd =~ s:/?[^/]*$::; | ||
| 66 | } | ||
| 67 | return $cwd; | ||
| 68 | } | ||
| 69 | |||
| 70 | 1; |