Download the PHP package timdev/timdev-log without Composer
On this page you can find all versions of the php package timdev/timdev-log. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download timdev/timdev-log
More information about timdev/timdev-log
Files in timdev/timdev-log
Package timdev-log
Short Description (Very) opinionated structured logging for PHP. Probably not for you.
License MIT
Informations about the package timdev-log
timdev/log
(Very) Opinionated, structured, logging for PHP. Probably not for you.
- Build on top of monolog and timdev/stack-logger.
- Logs are structured as ndjson
Goals
This package represents my (admittedly evolving) approach to logging for PHP applications. It composes timdev/stack-logger with monolog to produce a distinct flavor of ndjson logs.
The main goal is to reduce the number of things I need to decide or remember when setting up logging in PHP application. Therefore, configuration knobs are intentionally minimized.
The Logger
The logger extends timdev/stack-logger's MonologStackLogger, providing a static factory with only three scalar arguments (only one of which is required). It also provides a couple of convenience methods to help log application events and exceptions. And that's it.
Included Middleware
StackLogger opens up some nice possibilities. Particularly in middleware-based web applications, it can be nice to add some persistent context to the logger instance early in the request, so it's included in all subsequent logging calls.
This library includes several StackLogger-aware PSR-15-compatible middleware that I've used in web app projects.
TimDev\Log\Middleware\LogRequestAttributes
Extracts request attributes from the PSR7 ServerRequest and adds them as context to the logger.
Example:
The middleware will not set context keys for null
values.
For more example usage, see the tests
TimDev\Log\Middleware\DevelopmentRedirect
This middleware strips the Location
header from redirect-responses and
replaces the body with basic HTML document that includes a meta-refresh tag.
This is handy if you're using something like Monolog's BrowserConsoleHandler that relies on emitting javascript for the browser to execute in order to push log messages to the browser's console.
Example:
This middleware should usually be added early in your pipeline, since it only touches the response, and you want the response-mutation to happen last or nearly-last. In my projects, I typically do something like this:
Framework Integration
Mezzio
To date, I've been using this setup with Mezzio-based applications.
This package provides a ConfigProvider and a LoggerFactory.
To set this logger up in your mezzio project, just add the ConfigProvider in your config, and you'll have a logger in your container:
You can configure the logger in any of your config/autoload/*.php
files as
appropriate. A full configuration might look like:
config/autoload/timdev_log.local.php
All versions of timdev-log with dependencies
monolog/monolog Version ^2.3
psr/container Version ^1.1
psr/http-server-middleware Version ^1.0
timdev/stack-logger Version ^0.1.0
timdev/typed-config Version ^0.1.1