vxopdx
vxopdx

Reputation: 129

Log4perl: new message in the same line without prefix

Each Log4perl message starts with a new line:

my $log = Log::Log4perl->get_logger("log");
$log->info("start:");
$log->info("10");
$log->info("20");
$log->info("30");

result:

[2012/07/06 13:12:27] INFO log - start:
[2012/07/06 13:12:27] INFO log - 10
[2012/07/06 13:12:27] INFO log - 20
[2012/07/06 13:12:27] INFO log - 30

How can I get next messages on the same line and without prefix:

[2012/07/06 13:12:27] INFO log - start: 10 20 30

Any ideas?

UPD:

I tried to make progress bar for my program. But as a result I need this progress bar only on screen, not at logs.

In this way I did so:

$log->info("start");
printf("progress: ");
printf("10");
printf("20");
printf("30");

Thank you for your answers!

Upvotes: 0

Views: 423

Answers (2)

beresfordt
beresfordt

Reputation: 5222

If you need a progress bar there is Term::ProgressBar in cpan

Upvotes: 3

Birei
Birei

Reputation: 36262

Try using two different categories that both write logs to same place, the screen. Modify their PatternLayout and use one or the other when you want to add the date or only the message. You should also add the newline explicity:

Content of script.pl:

use warnings;
use strict;
use Log::Log4perl;

my $conf = qq| 
    log4perl.category.Stdout_nl=INFO, Date           
    log4perl.appender.Date=Log::Log4perl::Appender::Screen
    log4perl.appender.Date.layout=PatternLayout
    log4perl.appender.Date.layout.ConversionPattern=%d %p %m

    log4perl.category.Stdout=INFO, Number
    log4perl.appender.Number=Log::Log4perl::Appender::Screen
    log4perl.appender.Number.layout=PatternLayout
    log4perl.appender.Number.layout.ConversionPattern= %-3m
|;

Log::Log4perl::init( \$conf );
my $log_date = Log::Log4perl->get_logger( "Stdout_nl" );
my $log_num = Log::Log4perl->get_logger( "Stdout" );
$log_date->info("start:");
$log_num->info("10");
$log_num->info("20");
$log_num->info("30\n");

Run it like:

perl script.pl

With following output:

2012/07/06 12:24:04 INFO start:10 20 30

Upvotes: 3

Related Questions