Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I simply can't make Dancer2 write to a log file in a certain directory #1585

Open
JJ opened this issue Feb 4, 2021 · 3 comments
Open

I simply can't make Dancer2 write to a log file in a certain directory #1585

JJ opened this issue Feb 4, 2021 · 3 comments

Comments

@JJ
Copy link

JJ commented Feb 4, 2021

I finally deleted the config.yml because reasons #1584. So I try and set everything by hand:

package My::Hitos;

use v5.14;

use Dancer2;
use My::Hitos::Config qw($hitos %config);

for my $c (keys %config) {
  set $c => $config{$c}
}

set 'content_type' => 'application/json';
set 'engine' => 'logger';
set 'logger' => 'file';

say setting('log_dir');

get '/status' => sub {
  return to_json { status => 'OK' };
};

get '/all' => sub {
  return to_json $hitos;
};

"Rock'n'roll";

The main file is as in #1583, there's an additional config that sets values. Those mostly seem to work, so I don't think they are relevant (they only set log_dir). But this does not. The problem is not only that they don't work, is that they fail silently. It's quite clear that set can be used to set arbitrary key/value combination, so anything goes. But I'd like to know what I can do to set those things, and what combination of settings are needed to log elsewhere. The way it's set now, it simply does not log anywhere.

@1nickt
Copy link
Contributor

1nickt commented Mar 7, 2021

Hello @JJ

I'm sorry you are struggling. Note that by default the Dancer2 file logger will create a directory and files within your appdir.

If you're using the file logging engine, a directory appdir/logs will be created and will host one logfile per environment.

Also note that you may be overriding your config.yaml with your environment config files (development.yaml comes with logger set to console).

Also, note that the options for the logger go in the engines section of config (whereas the logger key is top-level).

So you need this in either your config.yaml or your environments/development.yaml (etc).

logger: "file"

engines:
  logger:
    File:
      log_dir: '/tmp'
      file_name: 'jj.log'

Note that it's best practise to configure your logger per environment (ie in the respective file) because you usually will want different logging outputs.

Hope this helps!

@JJ
Copy link
Author

JJ commented Mar 7, 2021

Thanks a lot for your answer, but can you indicate what's wrong above? Is there anything I'm not doing right? As you see, I want to log using JSON; the configuration you have inserted does not seem to cover that.

@1nickt
Copy link
Contributor

1nickt commented Mar 7, 2021

Hi @JJ

What's wrong with what you showed

set 'engine' => 'logger'; ... this is meaningless.

say setting('log_dir'); ... there is no such top-level setting.

set 'content_type' => 'application/json'; ... this does not affect logging.

How to configure your logger

I still think that you originally became confused because you had an environments/development.yaml config file (which is installed by default and uses the console logger by default) that was (correctly) overriding your config.yaml.

You should be able to configure your logger with directory and file name using the environments/{{env}}.yaml files: as I mentioned, it's usually most convenient to omit the logging config from config.yaml and place it in the environment config files.

Put this in environments/development.yaml and omit any logging config from config.yaml.

logger: 'file'

engines:
  logger:
    File:
      log_dir: '/tmp'
      file_name: 'jj.log'

Logging JSON

I recommend that you look into my modules Dancer2::Logger::LogAny (which replaces the default logger with Log::Any) and Log::Any::Adapter::JSON (which produces single-line JSON logging of aribitrary structured data).

After installing the packages your config would look something like:

logger : LogAny

engines :
    logger :
         LogAny :
             category : SomeName
             logger   :
                 - JSON
                 - /tmp/test.log

And your log would contain lines like:

{"category":"Net::Google::CivicInformation","level":"trace","message":"Building instance of Net::Google::CivicInformation::Representatives","time":"2021-03-07T06:57:41.63305"}
{"category":"ContactMyReps","level":"debug","message":"returning response","time":"2021-03-07T10:08:27.62779"}

You can see this configuration in use in a production app in the repo for ContactMyReps.com if you are interested.

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants