- Notifications
You must be signed in to change notification settings - Fork0
Use Monolog to log data to files/databases/notifications/...
License
bnomei/kirby3-monolog
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Use Monolog to log data to files/databases/notifications/...
- unzipmaster.zip as folder
site/plugins/kirby3-monolog
or git submodule add https://github.com/bnomei/kirby3-monolog.git site/plugins/kirby3-monolog
orcomposer require bnomei/kirby3-monolog
site/templates/home.php
monolog()->info('test-' .md5((string)time()), ['title' =>$page->title(),// field will be normalized'page' =>$page->id(),]);
site/logs/2024-10-27.log
[2024-10-27 19:10:30] default.INFO: test-d4a22afc0f735f551748d17c959b3339 {"title":"Home","page":"home"} []
Page-MethodThis plugin also registers a Page-Method which will use the UUID of the page as achannel
. You can use it like this:
$page->monolog()->info('test-' .md5((string)time()), [/*...*/]);
site/logs/2024-10-27.log
[2024-10-27 19:10:30] {UUID_of_page}.INFO: test-d4a22afc0f735f551748d17c959b3339 {} []
Use thedefault channel provided by this plugin or define your ownChannels. Monolog comes bundled with a lot of handlers, formatters and processors.
- write to file or syslogs
- send mails
- post to Slack/Discord
- insert into local or remote databases
- format as JSON
- append file/class/method Introspection
- append a UUID
- append URI, post method and IP
- ... create your own
// write to channel 'default' which writes to file// defined at 'bnomei.monolog.file' callback$log = \Bnomei\Log::singleton()->channel('default');// is same as$log =monolog('default');// or simply$log =monolog();// get a logger instance by channel by name$securityLogger =monolog('security');
$log =monolog();$log->warning('Foo');// or with method chainingmonolog()->error('Bar');
monolog()->info('Adding a new user', ['username' =>$user->name(),]);// increment Field `visits` in current Page$page =$page->increment('visits');monolog()->info('Incrementing Field', ['page' =>$page->id(),'visits' =>$page->visits()->toInt(),]);
Thedefault channel provided by this plugin writes file to thesite/logs
folder. It will be using the filename formatdate('Y-m-d') . '.log'
andnormalizes the data to make logging Kirby Objects easier.
Note
Without that normalization you would have to call->value()
or cast asstring
on every Kirby Field before adding its value as context data.
site/config/config.php
return [// other config settings ...// (optional) add custom channels'bnomei.monolog.channels' => ['security' =>function() {$logger =new \Monolog\Logger('security');// add handlers, formatters, processors and then...return$logger; } ],];
site/config/config.php
return [// other config settings ...// (optional) add custom channels from other plugins'bnomei.monolog.channels.extends' => ['myplugin.name.channels',// array of channel definitions in your other option ],];
This plugin is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, pleasecreate a new issue.
It is discouraged to use this plugin in any project that promotes racism, sexism, homophobia, animal abuse, violence or any other form of hate speech.
About
Use Monolog to log data to files/databases/notifications/...