How do I enable multiple loggers?

44 views
Skip to first unread message

Richard Sugg

unread,
Apr 22, 2014, 12:50:27 PM4/22/14
to mojol...@googlegroups.com
I have a couple of processes that do a lot of logging that I would not like a part of the main application's log file, but when I setup multiple loggers, they also seem to log to the same place.

# primary logger that logs to log/log1.log
my $logger1 = MojoX::Log::Log4perl->new( \$config1 );
$self->log($logger1);

# secondary logger for maint tasks should log to log/log2.log
my $logger2 = MojoX::Log::Log4perl->new( \$config2 );
$self->helper(log2 => sub { $logger2 });

# Instead of going to logs log1.log and log2.log respectively, they
# both go to log1.log
$self->app->log->info("this should be in log 1 but not log 2");
$self->app->log2->info("this should be in log 2 but not in log 1");

How do I enable multiple loggers?

Tekki

unread,
Apr 23, 2014, 9:00:18 AM4/23/14
to mojol...@googlegroups.com
Why don't you use Mojo::Log?

In startup:

  my $log1 = Mojo::Log->new(path => $path1);
  my $log2 = Mojo::Log->new(path => $path2);
  $self->helper(log1 => sub {return $log1});
  $self->helper(log2 => sub {return $log2});


And in the controller:

  $self->log1->info('I am in log 1');
  $self->log2->info('I am in log 2');

Richard Sugg

unread,
Apr 23, 2014, 9:32:28 AM4/23/14
to mojol...@googlegroups.com
I don't think you can customize the format of the logs, plus I use some of the Log4perl dispatchers to log to a database.

Tekki

unread,
Apr 23, 2014, 10:45:48 AM4/23/14
to mojol...@googlegroups.com

Am Mittwoch, 23. April 2014 15:32:28 UTC+2 schrieb Richard Sugg:
I don't think you can customize the format of the logs, plus I use some of the Log4perl dispatchers to log to a database.


 Ok. And if you replace Mojo::Log in my example with MojoX::Log::Log4perl, doesn't it work?

Richard Sugg

unread,
Apr 23, 2014, 11:10:04 AM4/23/14
to mojol...@googlegroups.com
no - even if I do both loggers as helpers, they both log to the first log file, and the second log file isn't even created.

Tekki

unread,
Apr 23, 2014, 11:48:02 AM4/23/14
to mojol...@googlegroups.com

Am Mittwoch, 23. April 2014 17:10:04 UTC+2 schrieb Richard Sugg:
no - even if I do both loggers as helpers, they both log to the first log file, and the second log file isn't even created.


That's correct. But this seems to be a Log4perl and not a Mojolicious question. Take a look at this:
http://stackoverflow.com/questions/6611586/how-to-get-log4perl-to-use-separate-files-for-each-script

Richard Sugg

unread,
Apr 23, 2014, 12:41:31 PM4/23/14
to mojol...@googlegroups.com
oh yes - you are right. You can only initialize Log4perl once, so even if I use different configs, it won't work. It all has to be in the same config. Thank you!
Reply all
Reply to author
Forward
0 new messages