0

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!

vxopdx
  • 129
  • 1
  • 8
  • 2
    $log->info("start: 10 20 30"); – beresfordt Jul 06 '12 at 09:20
  • 1
    I took a quick look at the Log4perl source. It looks like the `\n` are hardcoded in the `log` method. You could possibly [monkeypatch](http://en.wikipedia.org/wiki/Monkey_patch) it to not include the `\n`. *Mastering Perl* by brian d foy explains ways to that in chapter 10. – simbabque Jul 06 '12 at 09:47
  • @simbabque thank you for advice, but it`s overkill for my task – vxopdx Jul 06 '12 at 11:37

2 Answers2

3

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
Birei
  • 35,723
  • 2
  • 77
  • 82
3

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

beresfordt
  • 5,088
  • 10
  • 35
  • 43
  • +1 for suggesting the OP pound a nail with a hammer rather than with [an old shoe or a glass bottle](http://weblogs.asp.net/alex_papadimoulis/archive/2005/05/25/408925.aspx). – pilcrow Jul 06 '12 at 15:23