What is the correct way to use Mojo::Log?
Asked Answered
B

1

7

I would like to use Mojolicious' Mojo::Log tool to do logging in my Mojolicious web application. However, I am unsure of the proper/correct way to use it.

The documentation shows it used directly from the script, but fails to say if it's thread-safe or safe to share between controllers, or if each controller should instantiate its own Mojo::Log object (and in that case, will it be safe for all of them to point to the same log file?).

What is the correct way to use this logger?

Barbwire answered 9/7, 2019 at 22:52 Comment(0)
H
7

A Mojolicious application has a Mojo::Log object, accessible by the log attribute, which it uses for logging. Most controllers should be able to share this and its behavior will be set up according to the current mode, log level environment variables, etc. Mojolicious does not use threads itself but an IOLoop which is cooperative multitasking, nothing is actually running at the same time in a process; it uses flocked writes so it is safe from concurrency with other processes that do the same, such as in a prefork server like Hypnotoad with multiple workers writing to the same log.

# startup or plugin code
$app->log->debug('Debug message');

# controller or helper code
$c->app->log->info('Something happened');
Handwork answered 9/7, 2019 at 23:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.