Logger
in package
implements
LoggerInterface, ResettableInterface
Monolog log channel
It contains a stack of Handlers and a stack of Processors, and uses them to store records that are added to it.
Tags
Table of Contents
Interfaces
- LoggerInterface
- Describes a logger instance.
- ResettableInterface
- Handler or Processor implementing this interface will be reset when Logger::reset() is called.
Constants
- ALERT = 550
- Action must be taken immediately
- API = 3
- Monolog API version
- CRITICAL = 500
- Critical conditions
- DEBUG = 100
- Detailed debug information
- EMERGENCY = 600
- Urgent alert.
- ERROR = 400
- Runtime errors
- INFO = 200
- Interesting events
- NOTICE = 250
- Uncommon events
- WARNING = 300
- Exceptional occurrences that are not errors
- RFC_5424_LEVELS = [7 => \Monolog\Level::Debug, 6 => \Monolog\Level::Info, 5 => \Monolog\Level::Notice, 4 => \Monolog\Level::Warning, 3 => \Monolog\Level::Error, 2 => \Monolog\Level::Critical, 1 => \Monolog\Level::Alert, 0 => \Monolog\Level::Emergency]
- Mapping between levels numbers defined in RFC 5424 and Monolog ones
Properties
- $exceptionHandler : Closure|null
- $handlers : array<int, HandlerInterface>
- The handler stack
- $microsecondTimestamps : bool
- $name : string
- $processors : array<string|int, callable(LogRecord): LogRecord|ProcessorInterface>
- Processors that will process all log records
- $timezone : DateTimeZone
- $detectCycles : bool
- Whether to detect infinite logging loops This can be disabled via {@see useLoggingLoopDetection} if you have async handlers that do not play well with this
- $fiberLogDepth : WeakMap<Fiber<mixed, mixed>, int>
- $logDepth : int
- Keeps track of depth to prevent infinite logging loops
Methods
- __construct() : mixed
- __serialize() : array<string, mixed>
- __unserialize() : void
- addRecord() : bool
- Adds a log record.
- alert() : void
- Adds a log record at the ALERT level.
- close() : void
- Ends a log cycle and frees all resources used by handlers.
- critical() : void
- Adds a log record at the CRITICAL level.
- debug() : void
- Adds a log record at the DEBUG level.
- emergency() : void
- Adds a log record at the EMERGENCY level.
- error() : void
- Adds a log record at the ERROR level.
- getExceptionHandler() : Closure|null
- getHandlers() : array<int, HandlerInterface>
- getLevelName() : string
- Gets the name of the logging level as a string.
- getName() : string
- getProcessors() : array<string|int, callable>
- getTimezone() : DateTimeZone
- Returns the timezone to be used for the timestamp of log records.
- info() : void
- Adds a log record at the INFO level.
- isHandling() : bool
- Checks whether the Logger has a handler that listens on the given level
- log() : void
- Adds a log record at an arbitrary level.
- notice() : void
- Adds a log record at the NOTICE level.
- popHandler() : HandlerInterface
- Pops a handler from the stack
- popProcessor() : callable
- Removes the processor on top of the stack and returns it.
- pushHandler() : $this
- Pushes a handler on to the stack.
- pushProcessor() : $this
- Adds a processor on to the stack.
- reset() : void
- Ends a log cycle and resets all handlers and processors to their initial state.
- setExceptionHandler() : $this
- Set a custom exception handler that will be called if adding a new record fails
- setHandlers() : $this
- Set handlers, replacing all existing ones.
- setTimezone() : $this
- Sets the timezone to be used for the timestamp of log records.
- toMonologLevel() : Level
- Converts PSR-3 levels to Monolog ones if necessary
- useLoggingLoopDetection() : $this
- useMicrosecondTimestamps() : $this
- Control the use of microsecond resolution timestamps in the 'datetime' member of new records.
- warning() : void
- Adds a log record at the WARNING level.
- withName() : static
- Return a new cloned instance with the name changed
- handleException() : void
- Delegates exception management to the custom exception handler, or throws the exception if no custom handler is set.
Constants
ALERT
Action must be taken immediately
Use \Monolog\Level::Alert
public
mixed
ALERT
= 550
Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up.
API
Monolog API version
public
mixed
API
= 3
This is only bumped when API breaks are done and should follow the major version of the library
CRITICAL
Critical conditions
Use \Monolog\Level::Critical
public
mixed
CRITICAL
= 500
Example: Application component unavailable, unexpected exception.
DEBUG
Detailed debug information
Use \Monolog\Level::Debug
public
mixed
DEBUG
= 100
EMERGENCY
Urgent alert.
Use \Monolog\Level::Emergency
public
mixed
EMERGENCY
= 600
ERROR
Runtime errors
Use \Monolog\Level::Error
public
mixed
ERROR
= 400
INFO
Interesting events
Use \Monolog\Level::Info
public
mixed
INFO
= 200
Examples: User logs in, SQL logs.
NOTICE
Uncommon events
Use \Monolog\Level::Notice
public
mixed
NOTICE
= 250
WARNING
Exceptional occurrences that are not errors
Use \Monolog\Level::Warning
public
mixed
WARNING
= 300
Examples: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong.
RFC_5424_LEVELS
Mapping between levels numbers defined in RFC 5424 and Monolog ones
private
mixed
RFC_5424_LEVELS
= [7 => \Monolog\Level::Debug, 6 => \Monolog\Level::Info, 5 => \Monolog\Level::Notice, 4 => \Monolog\Level::Warning, 3 => \Monolog\Level::Error, 2 => \Monolog\Level::Critical, 1 => \Monolog\Level::Alert, 0 => \Monolog\Level::Emergency]
Tags
Properties
$exceptionHandler
protected
Closure|null
$exceptionHandler
= null
$handlers
The handler stack
protected
array<int, HandlerInterface>
$handlers
$microsecondTimestamps
protected
bool
$microsecondTimestamps
= true
$name
protected
string
$name
$processors
Processors that will process all log records
protected
array<string|int, callable(LogRecord): LogRecord|ProcessorInterface>
$processors
To process records of a single handler instead, add the processor on that specific handler
$timezone
protected
DateTimeZone
$timezone
$detectCycles
Whether to detect infinite logging loops This can be disabled via {@see useLoggingLoopDetection} if you have async handlers that do not play well with this
private
bool
$detectCycles
= true
$fiberLogDepth
private
WeakMap<Fiber<mixed, mixed>, int>
$fiberLogDepth
Keeps track of depth inside fibers to prevent infinite logging loops
$logDepth
Keeps track of depth to prevent infinite logging loops
private
int
$logDepth
= 0
Methods
__construct()
public
__construct(string $name[, array<string|int, HandlerInterface> $handlers = [] ][, array<string|int, callable> $processors = [] ][, DateTimeZone|null $timezone = null ]) : mixed
Parameters
- $name : string
-
The logging channel, a simple descriptive name that is attached to all log records
- $handlers : array<string|int, HandlerInterface> = []
-
Optional stack of handlers, the first one in the array is called first, etc.
- $processors : array<string|int, callable> = []
-
Optional array of processors
- $timezone : DateTimeZone|null = null
-
Optional timezone, if not provided date_default_timezone_get() will be used
Tags
__serialize()
public
__serialize() : array<string, mixed>
Return values
array<string, mixed>__unserialize()
public
__unserialize(array<string, mixed> $data) : void
Parameters
- $data : array<string, mixed>
addRecord()
Adds a log record.
public
addRecord(int $level, string $message[, array<string|int, mixed> $context = [] ][, DateTimeImmutable|null $datetime = null ]) : bool
Parameters
- $level : int
-
The logging level (a Monolog or RFC 5424 level)
- $message : string
-
The log message
- $context : array<string|int, mixed> = []
-
The log context
- $datetime : DateTimeImmutable|null = null
-
Optional log date to log into the past or future
Tags
Return values
bool —Whether the record has been processed
alert()
Adds a log record at the ALERT level.
public
alert(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
This method allows for compatibility with common interfaces.
Parameters
- $message : string|Stringable
-
The log message
- $context : array<string|int, mixed> = []
-
The log context
close()
Ends a log cycle and frees all resources used by handlers.
public
close() : void
Closing a Handler means flushing all buffers and freeing any open resources/handles. Handlers that have been closed should be able to accept log records again and re-open themselves on demand, but this may not always be possible depending on implementation.
This is useful at the end of a request and will be called automatically on every handler when they get destructed.
critical()
Adds a log record at the CRITICAL level.
public
critical(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
This method allows for compatibility with common interfaces.
Parameters
- $message : string|Stringable
-
The log message
- $context : array<string|int, mixed> = []
-
The log context
debug()
Adds a log record at the DEBUG level.
public
debug(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
This method allows for compatibility with common interfaces.
Parameters
- $message : string|Stringable
-
The log message
- $context : array<string|int, mixed> = []
-
The log context
emergency()
Adds a log record at the EMERGENCY level.
public
emergency(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
This method allows for compatibility with common interfaces.
Parameters
- $message : string|Stringable
-
The log message
- $context : array<string|int, mixed> = []
-
The log context
error()
Adds a log record at the ERROR level.
public
error(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
This method allows for compatibility with common interfaces.
Parameters
- $message : string|Stringable
-
The log message
- $context : array<string|int, mixed> = []
-
The log context
getExceptionHandler()
public
getExceptionHandler() : Closure|null
Return values
Closure|nullgetHandlers()
public
getHandlers() : array<int, HandlerInterface>
Return values
array<int, HandlerInterface>getLevelName()
Gets the name of the logging level as a string.
public
static getLevelName(int|Level $level) : string
Since 3.0, use toMonologLevel or \Monolog\Level->getName() instead
This still returns a string instead of a Level for BC, but new code should not rely on this method.
Parameters
- $level : int|Level
Tags
Return values
stringgetName()
public
getName() : string
Return values
stringgetProcessors()
public
getProcessors() : array<string|int, callable>
Tags
Return values
array<string|int, callable>getTimezone()
Returns the timezone to be used for the timestamp of log records.
public
getTimezone() : DateTimeZone
Return values
DateTimeZoneinfo()
Adds a log record at the INFO level.
public
info(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
This method allows for compatibility with common interfaces.
Parameters
- $message : string|Stringable
-
The log message
- $context : array<string|int, mixed> = []
-
The log context
isHandling()
Checks whether the Logger has a handler that listens on the given level
public
isHandling(int|string|Level $level) : bool
Parameters
- $level : int|string|Level
Tags
Return values
boollog()
Adds a log record at an arbitrary level.
public
log(mixed $level, string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
This method allows for compatibility with common interfaces.
Parameters
- $level : mixed
-
The log level (a Monolog, PSR-3 or RFC 5424 level)
- $message : string|Stringable
-
The log message
- $context : array<string|int, mixed> = []
-
The log context
Tags
notice()
Adds a log record at the NOTICE level.
public
notice(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
This method allows for compatibility with common interfaces.
Parameters
- $message : string|Stringable
-
The log message
- $context : array<string|int, mixed> = []
-
The log context
popHandler()
Pops a handler from the stack
public
popHandler() : HandlerInterface
Tags
Return values
HandlerInterfacepopProcessor()
Removes the processor on top of the stack and returns it.
public
popProcessor() : callable
Tags
Return values
callablepushHandler()
Pushes a handler on to the stack.
public
pushHandler(HandlerInterface $handler) : $this
Parameters
- $handler : HandlerInterface
Return values
$thispushProcessor()
Adds a processor on to the stack.
public
pushProcessor(ProcessorInterface|callable $callback) : $this
Parameters
- $callback : ProcessorInterface|callable
Tags
Return values
$thisreset()
Ends a log cycle and resets all handlers and processors to their initial state.
public
reset() : void
Resetting a Handler or a Processor means flushing/cleaning all buffers, resetting internal state, and getting it back to a state in which it can receive log records again.
This is useful in case you want to avoid logs leaking between two requests or jobs when you have a long running process like a worker or an application server serving multiple requests in one process.
setExceptionHandler()
Set a custom exception handler that will be called if adding a new record fails
public
setExceptionHandler(Closure|null $callback) : $this
The Closure will receive an exception object and the record that failed to be logged
Parameters
- $callback : Closure|null
Return values
$thissetHandlers()
Set handlers, replacing all existing ones.
public
setHandlers(array<int, HandlerInterface> $handlers) : $this
If a map is passed, keys will be ignored.
Parameters
- $handlers : array<int, HandlerInterface>
Return values
$thissetTimezone()
Sets the timezone to be used for the timestamp of log records.
public
setTimezone(DateTimeZone $tz) : $this
Parameters
- $tz : DateTimeZone
Return values
$thistoMonologLevel()
Converts PSR-3 levels to Monolog ones if necessary
public
static toMonologLevel(int|string|Level|LogLevel::* $level) : Level
Parameters
- $level : int|string|Level|LogLevel::*
-
Level number (monolog) or name (PSR-3)
Tags
Return values
LeveluseLoggingLoopDetection()
public
useLoggingLoopDetection(bool $detectCycles) : $this
Parameters
- $detectCycles : bool
Return values
$thisuseMicrosecondTimestamps()
Control the use of microsecond resolution timestamps in the 'datetime' member of new records.
public
useMicrosecondTimestamps(bool $micro) : $this
As of PHP7.1 microseconds are always included by the engine, so there is no performance penalty and Monolog 2 enabled microseconds by default. This function lets you disable them though in case you want to suppress microseconds from the output.
Parameters
- $micro : bool
-
True to use microtime() to create timestamps
Return values
$thiswarning()
Adds a log record at the WARNING level.
public
warning(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
This method allows for compatibility with common interfaces.
Parameters
- $message : string|Stringable
-
The log message
- $context : array<string|int, mixed> = []
-
The log context
withName()
Return a new cloned instance with the name changed
public
withName(string $name) : static
Parameters
- $name : string
Return values
statichandleException()
Delegates exception management to the custom exception handler, or throws the exception if no custom handler is set.
protected
handleException(Throwable $e, LogRecord $record) : void
Parameters
- $e : Throwable
- $record : LogRecord