Grafne has asked for the wisdom of the Perl Monks concerning the following question:

First off I hope the title gives the correct info.

I am using Log::Dispatch to write log files but need to be able to set the name of the log file to have the hostname included.

I am currently using a conf file to configure log::dispatch::config->Log::Dispatch::Config->configure_and_watch($file).

The file looks like this dispatchers = file # need to figure out how to generate name here.. file.class = Log::Dispatch::File file.min_level = debug file.filename = log/${HOST}.log file.mode = append file.size = 102400 file.max = 10 file.close_after_write = 1 file.format = [%d] [%p] [%P:%L]%m %n

I cant figure out how to get it to set the filename using the $HOST env var.

any ideas?

Replies are listed 'Best First'.
Re: using env vars in Log::Dispatch::Config config files
by roboticus (Chancellor) on Jun 19, 2012 at 16:38 UTC

    Grafne:

    print "$_:'$ENV{$_}'\n" for sort keys %ENV;

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      I am not sure what you mean?

      The data is stored in a config file. I could override this in my perl to over write the values in Log::Dispatch::Config->instance()->{'outputs'}->{'file'}->{'filename'} but this defeats the point of using the file. The intention is to allow users to edit the logger.conf file to change settings rather than having them hard coded.

      I am looking for a method of telling the log::dispatch::Config to use the env var to set the filename. I was hoping that the value read back from the file would be expanded when read and take the value of $HOST but instead the string is returned and I create a file called $host.log.

        Grafne:

        My bad. I interpreted your post as a "I can't get the environment variable" and I was showing how to access the environment variable hash. I didn't notice the part of your post when you're talking about reading it from a file. (I had thought that was a bit of the code you were having trouble with.)

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: using env vars in Log::Dispatch::Config config files
by toolic (Bishop) on Jun 22, 2012 at 15:03 UTC
    I've never used it, but from the Log::Dispatch SYNOPSIS, it looks like you can set the filename in your Perl code:
    my $log = Log::Dispatch->new( outputs => [ [ filename => "log/$ENV{HOST}.log" ], ], );
    Maybe you can set most of your parameters in the config file, and just this one in your Perl code.

      I am wondering if I can read back the filename, then expand it in my perl, then reassign it to the filename as you show above.

      I would like to set them all from the config file to keep things neat. Tidy code is good code ;-)