folded/log

Log information to various channels for your web app.

v0.1.2 2020-10-01 18:28 UTC

This package is auto-updated.

Last update: 2024-04-17 15:33:09 UTC


README

Log information to various channels for your web app.

Packagist License Packagist PHP Version Support Packagist Version Build Status Maintainability TODOs

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.

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. 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.

Version support

7.3 7.4 8.0
v0.1.0 ✔️
v0.1.1 ✔️