PHPackages                             kariricode/logging - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. kariricode/logging

ActiveLibrary[Framework](/categories/framework)

kariricode/logging
==================

A robust, flexible, and PSR-3 compliant logging component for the KaririCode Framework, providing comprehensive logging capabilities for PHP applications.

v1.2.0(9mo ago)010MITPHPPHP ^8.3CI failing

Since Sep 23Pushed 9mo agoCompare

[ Source](https://github.com/KaririCode-Framework/kariricode-logging)[ Packagist](https://packagist.org/packages/kariricode/logging)[ Docs](https://kariricode.org/)[ RSS](/packages/kariricode-logging/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (8)Versions (6)Used By (0)

KaririCode Framework: Logging Component
=======================================

[](#kariricode-framework-logging-component)

[![en](https://camo.githubusercontent.com/9687410941adb91c2f673c9d50ef38544f3e9a38a6b9f9367cac918a8d3e2a41/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c616e672d656e2d7265642e737667)](README.md)[![pt-br](https://camo.githubusercontent.com/03443f2a7a42ea03f0747d7bb784c5e42ee3f2c09e27bab66436a5021cc05c0c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c616e672d70742d2d62722d677265656e2e737667)](README.pt-br.md)

[![Docker](https://camo.githubusercontent.com/85e3ff712bb08b8e5595b34ecddfd189a51b20f61988aa467a56c5da9a107dda/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f446f636b65722d3234393645443f7374796c653d666f722d7468652d6261646765266c6f676f3d646f636b6572266c6f676f436f6c6f723d7768697465)](https://camo.githubusercontent.com/85e3ff712bb08b8e5595b34ecddfd189a51b20f61988aa467a56c5da9a107dda/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f446f636b65722d3234393645443f7374796c653d666f722d7468652d6261646765266c6f676f3d646f636b6572266c6f676f436f6c6f723d7768697465)[![Makefile](https://camo.githubusercontent.com/a04b8a060bf2e39eb19daa633fb06ec15cd6247035ce3c168127a37d7cadaad4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4d616b6566696c652d3144314431443f7374796c653d666f722d7468652d6261646765266c6f676f3d676e75266c6f676f436f6c6f723d7768697465)](https://camo.githubusercontent.com/a04b8a060bf2e39eb19daa633fb06ec15cd6247035ce3c168127a37d7cadaad4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4d616b6566696c652d3144314431443f7374796c653d666f722d7468652d6261646765266c6f676f3d676e75266c6f676f436f6c6f723d7768697465)[![PHP](https://camo.githubusercontent.com/d282cc3193faee11ee32307d0c4c9d809e8fafa4b3a8c12c6afbf35d4f7ec617/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d3737374242343f7374796c653d666f722d7468652d6261646765266c6f676f3d706870266c6f676f436f6c6f723d7768697465)](https://camo.githubusercontent.com/d282cc3193faee11ee32307d0c4c9d809e8fafa4b3a8c12c6afbf35d4f7ec617/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d3737374242343f7374796c653d666f722d7468652d6261646765266c6f676f3d706870266c6f676f436f6c6f723d7768697465)[![PHPUnit](https://camo.githubusercontent.com/1a56a0e0e8f226512ca8ebf712fae233ef290ab1acbe601f48ada7817583878b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f504850556e69742d3738453133303f7374796c653d666f722d7468652d6261646765266c6f676f3d706870756e6974266c6f676f436f6c6f723d7768697465)](https://camo.githubusercontent.com/1a56a0e0e8f226512ca8ebf712fae233ef290ab1acbe601f48ada7817583878b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f504850556e69742d3738453133303f7374796c653d666f722d7468652d6261646765266c6f676f3d706870756e6974266c6f676f436f6c6f723d7768697465)

A robust, flexible, and PSR-3 compliant logging component for the KaririCode Framework, providing comprehensive logging capabilities for PHP applications.

Features
--------

[](#features)

- PSR-3 compliant
- Supports multiple log channels (file, Slack, Papertrail, Elasticsearch)
- Log encryption
- Asynchronous logging support
- Query and performance logging
- Flexible logging formatters
- Supports log rotation and cleanup
- Circuit breaker and retry logic for logging
- Detailed context and structured logging

Installation
------------

[](#installation)

To install the KaririCode Logging component, run the following command:

```
composer require kariricode/logging
```

Basic Usage
-----------

[](#basic-usage)

### Step 1: Environment Configuration

[](#step-1-environment-configuration)

The **KaririCode Logging Component** relies on several environment variables to configure logging channels, log levels, external services, and other parameters. These variables are defined in a `.env` file, and the project comes with a default `.env.example` that should be copied to `.env` for initial setup.

To copy and create your `.env` file, run the following command:

```
make setup-env
```

This command will create a `.env` file if it doesn't already exist. Afterward, you can modify the values according to your requirements. Below are some key variables and their descriptions:

```
# Application environment (e.g., production, develop)
KARIRICODE_APP=develop

# PHP version and port used by the Docker service
KARIRICODE_PHP_VERSION=8.3
KARIRICODE_PHP_PORT=9303

# Default log channel (e.g., file, stderr, slack)
LOG_CHANNEL=file

# Log level (e.g., debug, info, warning, error)
LOG_LEVEL=debug

# Encryption key for log data (ensure this is kept secure)
LOG_ENCRYPTION_KEY=83302e6472acda6a8aeadf78409ceda3959994991393cdafbe23d2a46a148ba4

# Slack configuration for sending critical logs
SLACK_BOT_TOKEN=xoxb-your-bot-token-here
SLACK_CHANNEL=#your-channel-name

# Papertrail logging service configuration
PAPERTRAIL_URL=logs.papertrailapp.com
PAPERTRAIL_PORT=12345

# Formatter for logs written to stderr
LOG_STDERR_FORMATTER=json

# Elasticsearch index for storing logs
ELASTIC_LOG_INDEX=logging-logs

# Enable or disable asynchronous logging
ASYNC_LOG_ENABLED=true

# Enable or disable query logging, and configure thresholds
QUERY_LOG_ENABLED=true
QUERY_LOG_CHANNEL=file
QUERY_LOG_THRESHOLD=100

# Enable or disable performance logging, and configure thresholds
PERFORMANCE_LOG_ENABLED=true
PERFORMANCE_LOG_CHANNEL=file
PERFORMANCE_LOG_THRESHOLD=1000

# Enable or disable error logging
ERROR_LOG_ENABLED=true
ERROR_LOG_CHANNEL=file

# Log cleanup configuration (automatic removal of logs older than the specified number of days)
LOG_CLEANER_ENABLED=true
LOG_CLEANER_KEEP_DAYS=30

# Circuit breaker configuration for managing log retries
CIRCUIT_BREAKER_FAILURE_THRESHOLD=3
CIRCUIT_BREAKER_RESET_TIMEOUT=60

# Retry configuration for log failures
RETRY_MAX_ATTEMPTS=3
RETRY_DELAY=1000
RETRY_MULTIPLIER=2
RETRY_JITTER=100
```

Each of these variables can be adjusted according to your specific needs:

- **Log Channels:** You can choose between different logging channels such as `file`, `slack`, or `stderr`. For example, `LOG_CHANNEL=slack` will send critical logs to a Slack channel.
- **Log Levels:** This defines the minimum severity level for logs to be recorded (e.g., `debug`, `info`, `warning`, `error`, `critical`).
- **External Services:** If you want to send logs to external services like Slack or Papertrail, ensure you correctly set `SLACK_BOT_TOKEN`, `PAPERTRAIL_URL`, and `PAPERTRAIL_PORT`.

### Step 2: Loading Environment Variables and Configurations

[](#step-2-loading-environment-variables-and-configurations)

After configuring your `.env` file, you need to load the environment variables in your application and specify the path to the logging configuration file. This is done in the initialization of the application.

Here’s how to set it up in your `application.php` file:

```
