Constants

DEBUG

DEBUG = 100

Detailed debug information

INFO

INFO = 200

Interesting events

Examples: User logs in, SQL logs.

NOTICE

NOTICE = 250

Uncommon events

WARNING

WARNING = 300

Exceptional occurrences that are not errors

Examples: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong.

ERROR

ERROR = 400

Runtime errors

CRITICAL

CRITICAL = 500

Critical conditions

Example: Application component unavailable, unexpected exception.

ALERT

ALERT = 550

Action must be taken immediately

Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up.

EMERGENCY

EMERGENCY = 600

Urgent alert.

API

API = 2 : int

Monolog API version

This is only bumped when API breaks are done and should follow the major version of the library

RFC_5424_LEVELS

RFC_5424_LEVELS = [7 => self::DEBUG, 6 => self::INFO, 5 => self::NOTICE, 4 => self::WARNING, 3 => self::ERROR, 2 => self::CRITICAL, 1 => self::ALERT, 0 => self::EMERGENCY]

Mapping between levels numbers defined in RFC 5424 and Monolog ones

Properties

$levels

$levels : array<int,string>

This is a static variable and not a constant to serve as an extension point for custom levels

Type

array — Logging levels with the levels as key

$name

$name : string

Type

string

$handlers

$handlers : \Monolog\Handler\HandlerInterface[]

The handler stack

Type

HandlerInterface[]

$processors

$processors : callable[]

Processors that will process all log records

To process records of a single handler instead, add the processor on that specific handler

Type

callable[]

$microsecondTimestamps

$microsecondTimestamps : bool

Type

bool

$timezone

$timezone : \DateTimeZone

Type

\DateTimeZone —

$exceptionHandler

$exceptionHandler : callable|null

Type

callable|null

$logDepth

$logDepth : int

Type

int — Keeps track of depth to prevent infinite logging loops

$fiberLogDepth

$fiberLogDepth : \WeakMap<\Fiber,int>|null

Type

Fiber,int>|null — Keeps track of depth inside fibers to prevent infinite logging loops

$detectCycles

$detectCycles : bool

Type

bool — Whether to detect infinite logging loops This can be disabled via {@see \Monolog\useLoggingLoopDetection} if you have async handlers that do not play well with this

Methods

__construct()

__construct(string  $name, \Monolog\Handler\HandlerInterface[]  $handlers = [], callable[]  $processors = [], \DateTimeZone|null  $timezone = null) : mixed

Parameters

string $name

The logging channel, a simple descriptive name that is attached to all log records

\Monolog\Handler\HandlerInterface[] $handlers

Optional stack of handlers, the first one in the array is called first, etc.

callable[] $processors

Optional array of processors

\DateTimeZone|null $timezone

Optional timezone, if not provided date_default_timezone_get() will be used

Returns

mixed —

getName()

getName() : string

Returns

string —

withName()

withName(string  $name) : self

Return a new cloned instance with the name changed

Parameters

string $name

Returns

self —

pushHandler()

pushHandler(\Monolog\Handler\HandlerInterface  $handler) : self

Pushes a handler on to the stack.

Parameters

\Monolog\Handler\HandlerInterface $handler

Returns

self —

popHandler()

popHandler() : \Monolog\Handler\HandlerInterface

Pops a handler from the stack

Throws

\LogicException

If empty handler stack

Returns

\Monolog\Handler\HandlerInterface —

setHandlers()

setHandlers(\Monolog\Handler\HandlerInterface[]  $handlers) : self

Set handlers, replacing all existing ones.

If a map is passed, keys will be ignored.

Parameters

\Monolog\Handler\HandlerInterface[] $handlers

Returns

self —

getHandlers()

getHandlers() : \Monolog\Handler\HandlerInterface[]

Returns

\Monolog\Handler\HandlerInterface[] —

pushProcessor()

pushProcessor(callable  $callback) : self

Adds a processor on to the stack.

Parameters

callable $callback

Returns

self —

popProcessor()

popProcessor() : callable

Removes the processor on top of the stack and returns it.

Throws

\LogicException

If empty processor stack

Returns

callable —

getProcessors()

getProcessors() : callable[]

Returns

callable[] —

useMicrosecondTimestamps()

useMicrosecondTimestamps(bool  $micro) : self

Control the use of microsecond resolution timestamps in the 'datetime' member of new records.

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

bool $micro

True to use microtime() to create timestamps

Returns

self —

useLoggingLoopDetection()

useLoggingLoopDetection(bool  $detectCycles) : self

Parameters

bool $detectCycles

Returns

self —

addRecord()

addRecord(int  $level, string  $message, array  $context = [], \Monolog\DateTimeImmutable  $datetime = null) : bool

Adds a log record.

Parameters

int $level

The logging level (a Monolog or RFC 5424 level)

string $message

The log message

array $context

The log context

\Monolog\DateTimeImmutable $datetime

Optional log date to log into the past or future

Returns

bool —

Whether the record has been processed

close()

close() : void

Ends a log cycle and frees all resources used by handlers.

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.

reset()

reset() : void

Ends a log cycle and resets all handlers and processors to their initial state.

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.

getLevels()

getLevels() : array<string,int>

Gets all supported logging levels.

Returns

array

Assoc array with human-readable level names => level codes.

getLevelName()

getLevelName(int  $level) : string

Gets the name of the logging level.

Parameters

int $level

Throws

\Psr\Log\InvalidArgumentException

If level is not defined

Returns

string —

toMonologLevel()

toMonologLevel(string|int  $level) : int

Converts PSR-3 levels to Monolog ones if necessary

Parameters

string|int $level

Level number (monolog) or name (PSR-3)

Throws

\Psr\Log\InvalidArgumentException

If level is not defined

Returns

int —

isHandling()

isHandling(int  $level) : bool

Checks whether the Logger has a handler that listens on the given level

Parameters

int $level

Returns

bool —

setExceptionHandler()

setExceptionHandler(?callable  $callback) : self

Set a custom exception handler that will be called if adding a new record fails

The callable will receive an exception object and the record that failed to be logged

Parameters

?callable $callback

Returns

self —

getExceptionHandler()

getExceptionHandler() : ?callable

Returns

?callable —

log()

log(mixed  $level, string|\Stringable  $message, array  $context = []) : void

Adds a log record at an arbitrary level.

This method allows for compatibility with common interfaces.

Parameters

mixed $level

The log level (a Monolog, PSR-3 or RFC 5424 level)

string|\Stringable $message

The log message

array $context

The log context

debug()

debug(string|\Stringable  $message, array  $context = []) : void

Adds a log record at the DEBUG level.

This method allows for compatibility with common interfaces.

Parameters

string|\Stringable $message

The log message

array $context

The log context

info()

info(string|\Stringable  $message, array  $context = []) : void

Adds a log record at the INFO level.

This method allows for compatibility with common interfaces.

Parameters

string|\Stringable $message

The log message

array $context

The log context

notice()

notice(string|\Stringable  $message, array  $context = []) : void

Adds a log record at the NOTICE level.

This method allows for compatibility with common interfaces.

Parameters

string|\Stringable $message

The log message

array $context

The log context

warning()

warning(string|\Stringable  $message, array  $context = []) : void

Adds a log record at the WARNING level.

This method allows for compatibility with common interfaces.

Parameters

string|\Stringable $message

The log message

array $context

The log context

error()

error(string|\Stringable  $message, array  $context = []) : void

Adds a log record at the ERROR level.

This method allows for compatibility with common interfaces.

Parameters

string|\Stringable $message

The log message

array $context

The log context

critical()

critical(string|\Stringable  $message, array  $context = []) : void

Adds a log record at the CRITICAL level.

This method allows for compatibility with common interfaces.

Parameters

string|\Stringable $message

The log message

array $context

The log context

alert()

alert(string|\Stringable  $message, array  $context = []) : void

Adds a log record at the ALERT level.

This method allows for compatibility with common interfaces.

Parameters

string|\Stringable $message

The log message

array $context

The log context

emergency()

emergency(string|\Stringable  $message, array  $context = []) : void

Adds a log record at the EMERGENCY level.

This method allows for compatibility with common interfaces.

Parameters

string|\Stringable $message

The log message

array $context

The log context

setTimezone()

setTimezone(\DateTimeZone  $tz) : self

Sets the timezone to be used for the timestamp of log records.

Parameters

\DateTimeZone $tz

Returns

self —

getTimezone()

getTimezone() : \DateTimeZone

Returns the timezone to be used for the timestamp of log records.

Returns

\DateTimeZone —

__serialize()

__serialize() : array<string,mixed>

Returns

array

__unserialize()

__unserialize(array<string,mixed>  $data) : void

Parameters

array $data

handleException()

handleException(\Throwable  $e, array  $record) : void

Delegates exception management to the custom exception handler, or throws the exception if no custom handler is set.

Parameters

\Throwable $e
array $record