Logging in Pionia
At its core, Pionia uses the Monolog library to handle logging.
Initialization
Logging is inbuilt in Pionia. To view the logs in real-time, you can run the following command in a different terminal window pointing to your log file, which is by default server.log
:
Usage
Anywhere in your app, just call the logger
constant to log messages. The following log levels are available:
The above code will log messages at different levels. You can then view the logs in the server.log
file.
Customization
Log File
You can change the file to log to by defining the log destination in the settings.ini
file:
If you want to log to php://stdout, you can define the log destination as stdout
.
Log Format
You can also change the log format by defining the log format in the settings.ini
file:
Log formats can be TEXT
or JSON
.
Logging Settings
You can define the sections of settings.ini
you want to log along:
Securing Log Entries
You can hide sensitive information from the logs by defining the sensitive fields in the settings.ini
file:
The above means that whenever a log entry contains any of the fields password
, pin
, or acc
, the value will be replaced with *******
.
You can also define the string to replace the sensitive fields with:
Turning off logs
You can simply turn off DEBUG
mode in the settings.ini
file:
However much this is okay, but sometimes, if not most times, we would want to leave the logs on even when we are not in DEBUG
mode.
The following settings can be defined in the settings.ini
file:
Logging Requests
You can log requests by defining the LOG_REQUESTS
setting in the settings.ini
file:
Whether in DEBUG
or not, if the above is turned on, all requests will be logged.
By default, this attempts to log both the requests and responses. However, Responses are sometimes heavy and you may choose to omit them. You can do so by defining the LOG_RESPONSES
setting in the settings.ini
file:
Also, defining the LOG_RESPONSES
setting as true
will log only the responses without the requests.
With version 1.1.4, you can also define which Monolog processors you would like to include.
You can achieve this by defining the LOG_PROCESSORS
setting in the settings.ini
file:
By default, no processor is added to the logger.
With the above, you app now logs the process id and memory usage in the logs.
Renaming the logs.
By default, all logs indicate the pionia
prefix. You can change this by defining the APP_NAME
setting in the settings.ini
file:
The above will change the prefix to blog
.
Log Rotation
Pionia avoids dictating how you should handle log rotation. You can use the logrotate
utility to rotate logs. Here is an example configuration for log rotation:
There is a cool article about handling log rotation here.
That’s it! You now know how to log in Pionia.