folded / log
Log information to various channels for your web app.
Requires
- php: >=7.4.0
- monolog/monolog: 2.*
Requires (Dev)
- friendsofphp/php-cs-fixer: 2.*
- pestphp/pest: 0.3.*
- phpunit/phpunit: 9.*
This package is auto-updated.
Last update: 2024-10-17 16:50:01 UTC
README
Log information to various channels for your web app.
Summary
About
I created this package to simply setup Monolog, usable in any projects easily.
Folded is a constellation of packages to help you setting up a web app easily, using ready to plug in packages.
- folded/action: A way to organize your controllers for your web app.
- folded/config: Configuration utilities for your PHP web app.
- folded/crypt: Encrypt and decrypt strings for your web app.
- folded/exception: Various kind of exception to throw for your web app.
- folded/history: Manipulate the browser history for your web app.
- folded/http: HTTP utilities for your web app.
- folded/orm: An ORM for you web app.
- folded/session: Session functions for your web app.
- folded/request: Request utilities, including a request validator, for your PHP web app.
- folded/routing: Routing functions for your PHP web app.
- folded/view: View utilities for your PHP web app.
Features
- Uses monolog/monolog
- Can add multiple "write channels" (example: file, ...) to the same logger
- Supports the following channels:
- File (e.g. StreamHandler)
Requirements
- PHP version >= 7.4.0
- Composer installed
Installation
1. Install the package
In your root folder, run this command:
composer require folded/log
Examples
- 1. Log a debug message to a file
- 2. Add extra parameters when logging
- 3. Log by choosing your severity manually
- 4. Add another channel to the same logger
1. Create a file logger
In this example, we will create a file logger, and then we will log a debug information into our log file.
use function Folded\addLogger; use function Folded\addDebugLog; addLogger("myLogger", "file", [ "path" => __DIR__ . "/logs/my-file.log", ]); addDebugLog("myLogger", "This is my first debug log!");
Note that if you want to avoid typos, you can use constants for defining the file channel.
use function Folded\addLogger; use function Folded\addDebugLog; use const Folded\CHANNEL_FILE; addLogger("myLogger", CHANNEL_FILE, [ "path" => __DIR__ . "/logs/my-file.log", ]); addDebugLog("myLogger", "This is my first debug log!");
For the moment, there is only one channel:
use const Folded\CHANNEL_FILE;
You can log various severities. Here is all the log methods available:
- addAlertLog
- addCriticalLog
- addDebugLog
- addEmergencyLog
- addErrorLog
- addInfoLog
- addNoticeLog
- addWarningLog
2. Add extra parameters when logging
In this example, we will push extra data as key-value pairs alongisde our log message.
use function Folded\addLogger; use function Folded\addDebugLog; addLogger("myLogger", "file", [ "path" => __DIR__ . "/logs/my-file.log", ]); addDebugLog("myLogger", "a user has registered", [ "timestamp" => 1577836800, ]);
3. Log by choosing your severity manually
In this example, we will use a addLog
function, which let us choose the severity using a string for more flexibility.
use function Folded\addLogger; use function Folded\addLog; addLogger("myLogger", "file", [ "path" => __DIR__ . "/logs/neutral.log", ]); addLog("myLogger", "warning", "User email invalid");
Note you can use severity constants provided by this library if you want to avoid typos:
use function Folded\addLogger; use function Folded\addLog; use const Folded\SEVERITY_WARNING; addLogger("myLogger", "file", [ "path" => __DIR__ . "/logs/neutral.log", ]); addLog("myLogger", SEVERITY_WARNING, "User email invalid");
Here is a complete list of severities you can use:
use const Folded\SEVERITY_ALERT; use const Folded\SEVERITY_CRITICAL; use const Folded\SEVERITY_DEBUG; use const Folded\SEVERITY_EMERGENCY; use const Folded\SEVERITY_ERROR; use const Folded\SEVERITY_INFO; use const Folded\SEVERITY_NOTICE; use const Folded\SEVERITY_WARNING;
4. Add another channel to the same logger
In this example, our logger will log to 2 differents files.
use function Folded\addLogger; use function Folded\addLoggerChannel; addLogger("myLogger"); // First file addLoggerChannel("myLogger", "file", [ "path" => "path/to/file-1.log", ]); // Second file addLoggerChannel("myLogger", "file", [ "path" => "path/to/file-2.log", ]);
Now, everytime you call a log method like addDebugLog()
, the log will be written in these two files.
This is very powerful when this library will be able to provide with different kind of channels, so you will be able to write both in a file and in your Syslog channel for example.