[dancer-users] Best way to log from wherever?

Aaron Hall ahall at vitaphone.net
Tue Jan 5 08:06:59 EST 2021

Hi all, Happy New Year. :)

What is the current thinking on the "best" or "right" way to log from
all parts of your program? I wrestle with this occasionally. I'm not a
programmer by trade (these are mainly personal/hobby projects, although
I have done stuff for $WORK before), so it seems to me that maybe
there's some best practice that I just haven't picked up on.

I want to be able to log from any part of my program, have all the logs
go through the same processing, and come out in the same place, with a
minimum of fuss. Preferably using the same simple
debug/info/warning/error APIs (from the logging client's point of view).
For the web-facing portions of the app, that's simple enough:
"use Dancer2 appname => ..." and be happy.

By "any part of my program", I'm thinking of code that doesn't use
Dancer2 and so doesn't get the DSL.

* Methods in DBIx::Class::Result classes. (Mainly for debugging.)

* app.psgi, where I might add a middleware that wants to log something.

* Utility modules that don't handle routes and don't use Dancer2. (Maybe
   they should anyway?)
These days, I usually make a "MyApp::Log" module and do something like:

     package MyApp::Log;
     use Dancer2 appname => 'MyApp::App';
     use Exporter qw/import/;
     our @EXPORT = qw/debug info warning error/;

That actually works pretty nicely; should I just do that? It feels like
if that's supposed to be the way, it wouldn't require playing tricks
with import().

Once I went so far as to write a Dancer2::Logger::App (never uploaded
anywhere) that would call my app back to log, and consolidating the
output stream that way. That was kinda fun, but after I did it I felt
like I'd re-invented the wheel inside-out.

I know there's a giant ecosystem of logging modules on CPAN. I've even
played with Log::Any. I suspect the solution involves using one of them,
but talking about the data flows gets so abstract I have trouble getting
my head around how to use them to make the various sources send logs to
the same place, where to configure it all from, and so on.

Can someone help straighten me out on this? :)

- Aaron

Aaron Hall                       Sleep, where is thy sting?
vitaphone.net                    Bed, where is thy victory?

More information about the dancer-users mailing list