programster / pgsql-logger
A PSR-3 logger that logs to a PostgreSQL database, creating its own table.
Requires
- php: ^8.2
- ext-pgsql: *
- programster/log: ^4.0
- programster/pgsql-lib: ^3.2
- psr/clock: ^1.0
- psr/log: ^3.0
Requires (Dev)
- phpstan/phpstan: ^2.1
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
What it's for
A PSR-3 logger that writes logs to a PostgreSQL table, which it can create for
you (in any schema), or give you the SQL for, to run in your own migrations.
Each log has a UUIDv7 ID (so ordering by it gives the order they were written in), its message, its level, its context
as jsonb (so you can query it), and when it was logged. It's safe to use in long-running processes, such as workers
and daemons, as it reconnects if the database restarts.
It extends programster/log's AbstractLogger, so it works with
that package's other loggers, e.g. as one of a FailoverLogger's loggers.
Requirements
- PHP 8.2, 8.3, 8.4 or 8.5, with the
pgsqlextension. - PostgreSQL 14, 15, 16, 17 or 18.
Installation
composer require programster/pgsql-logger
Quick start
use Programster\PgsqlLogger\PgSqlLogger; $connection = pg_connect("host=localhost dbname=app user=app password=secret"); // Create the "log" table (and its "log_level" type), if they don't exist, e.g. when the application starts. Or put // the SQL from PgSqlLogger::getCreateTablesSql() in your own migrations. PgSqlLogger::createTables($connection); $logger = new PgSqlLogger($connection); $logger->info("A user signed in.", ['userId' => 5]); $logger->warning("The disk is nearly full.", ['freeBytes' => 1024]); try { throw new RuntimeException("The payment provider timed out."); } catch (RuntimeException $exception) { $logger->error("A payment failed.", ['exception' => $exception]); }
See Logging for creating the table in your own migrations or another schema, levels, context, a custom clock, and reconnecting.
Design principles
- Your migrations, or ours.
createTables()creates the table when your application starts, orgetCreateTablesSql()gives you the SQL to run in your own migrations. Either is safe to run again. A logger created before its table exists throws straight away, saying how to create it, rather than at the first log. - A log is never lost to bad characters. Invalid UTF-8 and NUL characters, which PostgreSQL can't store, are replaced with U+FFFD (the replacement character), in the message and the context.
- Failures throw. A log that can't be written throws an exception, rather than being dropped silently, so it can be
caught (e.g. by a
FailoverLogger, to log somewhere else). - IDs are in the order logs were written, even if your application's clock moves, so you can page through logs by ID.
Documentation
The docs folder has the full documentation:
- Logging: creating the table (or getting the SQL for your migrations), schemas, levels, context, characters PostgreSQL can't store, a custom clock, reconnecting, and errors.
- Testing: running the package's tests.
- Upgrading: what changed in each release.
Licence
MIT. See LICENSE.