Logger addon for Cockpit CMS
This addon extends Cockpit CMS (Next) core by providing logging functionality based on the awesome Monolog Library.
Installation can be performed with ot without php composer.
$ cd your-cockpit-docroot/addons/Logger
$ composer install
https://your-cockpit-site/settings/logger
and confirm that page loads.
$ cd your-cockpit-docroot/addons
$ composer create-project pauloamgomes/logger Logger
The Logger operations are available to the super admin user, and they can be used by other users if they belong to a group with proper permissions. The following permissions are defined:
Above ACLs can be added to the global configuration file as below:
groups:
# manage logger settings
managers:
Logger:
manage.admin: true
# view log entries
editors:
Logger:
manage.view: true
The settings page provide all the main configurations:
Enable - Check that option to enable the logging
Context attributes - included in the logging entry as an extra array
Log Level - Set the log level
Cockpit Logger uses the same log levels defined in Monolog:
When its set to DEBUG, additional details about the request will be included in the log entry:
The Monolog sections are resumed to the Formatter and Handler.
A formatter defines the structure of the log entries,
Cockpit Logger supports three Monolog formatters
Formatter | Purpose |
---|---|
LineFormatter | Formats a log record into a one-line string. |
JsonFormatter | Formats a log record into json |
HtmlFormatter | Used to format log records into a human readable html table, mainly suitable for emails. |
Its also possible to define the format of the date in the log entries, the value must be a valid PHP date format like Y-m-d Hs
A handler defines how the logs will be saved. Cockpit Logger provides two Handlers:
Handler | Purpose |
---|---|
StreamHandler | Saves log entries in the filesystem using the configured location and filename |
SyslogHandler | Writes the log entries using the operating system syslog functionality. Requires an ident and syslog facility. |
SyslogUdpHandler | Pushes the log entries to a remove rsyslog server. |
The StreamHandler requires two additional settings:
The SyslogHandler requires also to additional settings:
Besides the configuration page the settings can also be defined in the cockpit config/config.yaml file, e.g.:
# Logger addon
logger:
enabled: 1,
level: INFO
handler: StreamHandler
formatter: LineFormatter
context:
user: 1
hostname: 0
http_method: 1
referrer: 1
request_uri: 1
When using the config.yaml, the settings will take precedence over the configuration page.
Most relevant Cockpit events (e.g. User login, Collection removal) are logged automatically:
If required to disable, they can be disabled in the config.yaml, e.g:
logger:
disabledEvents:
- collections.save.after
- collections.remove.after
[2018-09-08 23:06:20] cockpit.NOTICE: Collection entry saved {"_id":"5aa5024609677doc2021128895","collection":"simpleblock","isUpdate":true,"user":"admin","hostname":"traefik_global.docker_default","request_uri":"/collections/save_entry/simpleblock","http_method":"POST"}
[2018-09-08 23:06:46] cockpit.NOTICE: Collection entry saved {"_id":"5aa5024609677doc2021128895","collection":"simpleblock","isUpdate":true,"user":"admin","hostname":"traefik_global.docker_default","request_uri":"/collections/save_entry/simpleblock","http_method":"POST","debug":{"duration_time":"0.007 Sec","memory_usage":"2 MB","loaded_files":67}}
{
"channel": "cockpit",
"context": {
"_id": "5aa5024609677doc2021128895",
"collection": "simpleblock",
"debug": {
"duration_time": "0.008 Sec",
"loaded_files": 67,
"memory_usage": "2 MB"
},
"hostname": "traefik_global.docker_default",
"http_method": "POST",
"isUpdate": true,
"request_uri": "/collections/save_entry/simpleblock",
"user": "admin"
},
"datetime": {
"date": "2018-09-08 23:09:10.434059",
"timezone": "UTC",
"timezone_type": 3
},
"extra": [],
"level": 250,
"level_name": "NOTICE",
"message": "Collection entry saved"
}
The Cockpit Logger can be used on any other code by just invoking the module, e.g.:
$this->app->module('logger')->info('writing a new log entry', ['hello' => 'world']);
$this->app->module('logger')->warning('something weird happened');
$this->app->module('logger')->error('something very wrong happened', ['error' => $e]);
Logs can be accessed on the server and processed using any tool, additionaly its also possible to access from the UI
to the most recent log entries - https://your-cockpit-site/recent-logs
.
The UI supports automatic fetch and dynamic filters:
When using the StreamLogger and the log location is set to the #storage:logs
ensure that file is not public accessible.
Copyright 2018 pauloamgomes under the MIT license.