Open JJ opened 3 years ago
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!
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.
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!
I finally deleted the
config.yml
because reasons #1584. So I try and set everything by hand: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.