Custom log file configuration #207

Closed
IvanMartinez opened this Issue Feb 24, 2014 · 11 comments

Projects

None yet

2 participants

@IvanMartinez

Hello all,
Sorry if this is the wrong place for usage questions, I didn't find any other.
I'm trying to use my own custom log file, which I pretty much achieved by doing:

lager:trace_file("log/trade.log", [{type, trade}], info).

and then:

lager:info([{type, trade}], Message).

I can't see why custom log files are defined with a runtime command (lager:trace_file/3) and not in the application configuration like the standard ones (debug, info, error...). Can I use custom formatting with such files?. If yes, how?. Are they rotated too?.

Thank you,
Iván

@Vagabond

I think you can do this by creating logfiles with loglevel 'none' and then storing a persistent trace in the application config. It is not well documented but there is an example here:

https://github.com/basho/lager/blob/master/test/lager_test_backend.erl#L443

@Vagabond

The trace target in the app config would look like:

{traces, [{{lager_file_backend, "my_file_name.log"}, Filters, Level},...]}

@IvanMartinez

Thank you Andrew. I have tried with

  {lager, [
    {handlers, [
      {lager_file_backend, [{file, "error.log"}, {level, error}]},
      {lager_file_backend, [{file, "info.log"}, {level, info}]}
    ]},
    {traces, [
      {{lager_file_backend, "trade.log"}, [{type, trade}], info}]}
  ]}

I have tried with {level, info}, {level, none}, info and none as level, but no log is created.
The point of doing this is to be able to use custom formatting. How would you add it to the trace configuration?.

@Vagabond

You need a matching handler, it currently will not create it for you.

@IvanMartinez

Thank you again. With the configuration above, I create the log with:

lager:trace_file("log/trade.log", [{type, trade}], info).

So the log is generated, but how do I add custom formatting and file rotation?.

@Vagabond

No no, you need a handlers line that will create the file and configure rotation, but have its log level be 'none':

  {lager, [
    {handlers, [
      {lager_file_backend, [{file, "trade.log"}, {level, none}, {count 5}]},
      ...
    ]},
    {traces, [
      {{lager_file_backend, "trade.log"}, [{type, trade}], info}]}
  ]}

Then the logfile is created by lager at startup and the trace is installed.

@IvanMartinez

Sorry Andrew, but I can't get it to work. I have configured it like this:

  {lager, [
    {handlers, [
      {lager_file_backend, [{file, "error.log"}, {level, error}]},
      {lager_file_backend, [{file, "info.log"}, {level, info}]},
      {lager_file_backend, [{file, "bad_request.log"}, {level, none}, {count, 5}]}
    ]},
    {traces, [
      {{lager_file_backend, "bad_request.log"}, [{type, bad_request}], info}]}
  ]}

And then I try to log events like this:

lager:info([{type, bad_request}], "NoResource1\t~s\t~s", 
             [wrq:raw_path(RD), auth_id(RD)]),

But I can't see a bad_request.log file anywhere. Thank you for your help with this.

@Vagabond

can you show me the output of lager:status() when it is running?

@IvanMartinez
lager:status().
Lager status:
File log/console.log at level info
File log/error.log at level error
Console at level info
Active Traces:
Tracing Reductions:
Tracing Statistics:
  input: 0
 output: 0
 filter: 0
ok

I'm not using lager:trace_file/3, I understood I shouldn't.

@Vagabond

Double check you config because I was able to configure it like this:

Lager status:
File bad_request.log at level none
File log/console.log at level info
File log/error.log at level error
Console at level info
Active Traces:
Tracing messages matching {all,[{type,'=',bad_request}]} at level info to {lager_file_backend,
                                                                           "bad_request.log"}
Tracing Reductions:
Tracing Statistics:
  input: 1
 output: 0
 filter: 1
        [{handlers,$
            [{lager_console_backend,info},$
            {lager_file_backend,$
                [{file,"log/error.log"},$
                {level,error},$
                {size,10485760},$
                {date,"$D0"},$
                {count,5}]},$
            {lager_file_backend,$
                [{file,"log/console.log"},$
                {level,info},$
                {size,10485760},$
                {date,"$D0"},$
                {count,5}]},$
            {lager_file_backend, [{file, "bad_request.log"}, {level, none}, {count, 5}]}$
            ]},$
        {traces, [$
            {{lager_file_backend, "bad_request.log"}, [{type, bad_request}], info}]},$
        {colored,false},$
        ...
@IvanMartinez

I'm very sorry, I was putting the configuration in the wrong place (myapp.app.src instead of myapp.config).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment