Skip to content

Phalcon logger

NOTE

All classes are prefixed with Phalcon

Logger\AbstractLogger

Abstract Source on GitHub

Abstract Logger Class

Abstract logger class, providing common functionality. A formatter interface is available as well as an adapter one. Adapters can be created easily using the built in AdapterFactory. A LoggerFactory is also available that allows developers to create new instances of the Logger or load them from config files (see Phalcon\Config\Config object).

@property AdapterInterface[] $adapters @property array $excluded @property int $logLevel @property string $name @property string $timezone

Uses DateTimeImmutable · DateTimeZone · Exception · Phalcon\Logger\Adapter\AdapterInterface · Phalcon\Logger\Exceptions\AdapterNotFound · Phalcon\Logger\Exceptions\NoAdaptersConfigured · Phalcon\Time\Clock\ClockInterface · Phalcon\Time\Clock\SystemClock

Method Summary

Constants

int ALERT = 2
int CRITICAL = 1
int CUSTOM = 8 Default threshold and fallback sink. It sits between DEBUG (7) and TRACE (9) in the ordering, so the default log level excludes TRACE. It is also the fallback for unknown message levels and invalid setLogLevel() values.
int DEBUG = 7
int EMERGENCY = 0
int ERROR = 3
int INFO = 6
int NOTICE = 5
int TRACE = 9
int WARNING = 4

Properties

protected AdapterInterface[] $adapters = [] The adapter stack
protected ClockInterface $clock Clock used to timestamp log items
protected array $excluded = [] The excluded adapters for this log process
protected int $logLevel = 8 Minimum log level for the logger
protected string $name = ""
protected DateTimeZone $timezone

Methods

Public · 13

__construct()

public function __construct(
    string $name,
    array $adapters = [],
    DateTimeZone $timezone = null,
    ClockInterface $clock = null
);

Constructor.

addAdapter()

public function addAdapter(
    string $name,
    AdapterInterface $adapter
): static;

Add an adapter to the stack. For processing we use FIFO

begin()

public function begin(): static;

Starts a transaction on every (non-excluded) adapter in the stack.

commit()

public function commit(): static;

Commits the transaction on every (non-excluded) adapter in the stack.

excludeAdapters()

public function excludeAdapters( array $adapters = [] ): static;

Exclude certain adapters.

getAdapter()

public function getAdapter( string $name ): AdapterInterface;

Returns an adapter from the stack

getAdapters()

public function getAdapters(): array;

Returns the adapter stack array

getLogLevel()

public function getLogLevel(): int;

Returns the log level

getName()

public function getName(): string;

Returns the name of the logger

removeAdapter()

public function removeAdapter( string $name ): static;

Removes an adapter from the stack

rollback()

public function rollback(): static;

Rolls back the transaction on every (non-excluded) adapter in the stack.

setAdapters()

public function setAdapters( array $adapters ): static;

Sets the adapters stack overriding what is already there

setLogLevel()

public function setLogLevel( int $level ): static;

Sets the minimum log level for the logger.

An unknown level is not rejected: it is stored as CUSTOM, which sits between DEBUG and TRACE in the ordering, so the threshold becomes "everything except TRACE".

Protected · 3

addMessage()

protected function addMessage(
    int $level,
    string $message,
    array $context = []
): bool;

Adds a message to each handler for processing

getLevelNumber()

protected function getLevelNumber( mixed $level ): int;

Converts the level from string/word to an integer

getLevels()

protected function getLevels(): array;

Returns an array of log levels with integer to string conversion

Logger\AdapterFactory

Class Source on GitHub

Factory used to create adapters used for Logging

Uses Phalcon\Factory\AbstractFactory · Phalcon\Logger\Adapter\AdapterInterface · Phalcon\Logger\Exception

Method Summary

Methods

Public · 2

__construct()

public function __construct( array $services = [] );

AdapterFactory constructor.

newInstance()

public function newInstance(
    string $name,
    string $fileName,
    array $options = []
): AdapterInterface;

Create a new instance of the adapter

Protected · 2

getExceptionClass()

protected function getExceptionClass(): string;

getServices()

protected function getServices(): array;

Returns the available adapters

Logger\Adapter\AbstractAdapter

Abstract Source on GitHub

Class AbstractAdapter

@property string $defaultFormatter @property FormatterInterface $formatter @property bool $inTransaction @property array $queue

Uses Phalcon\Logger\Exceptions\DeserializationFailed · Phalcon\Logger\Exceptions\SerializationFailed · Phalcon\Logger\Exceptions\TransactionAlreadyActive · Phalcon\Logger\Exceptions\TransactionNotActive · Phalcon\Logger\Formatter\FormatterInterface · Phalcon\Logger\Formatter\Line · Phalcon\Logger\Item

Method Summary

Properties

protected string $defaultFormatter = "Phalcon\\Logger\\Formatter\\Line" Name of the default formatter class
protected FormatterInterface|null $formatter = null Formatter
protected bool $inTransaction = false Tells if there is an active transaction or not
protected array $queue = [] Array with messages queued in the transaction
protected int $queueLimit = 0 Maximum number of items retained in the transaction queue. 0 (default) keeps the original unbounded behavior; a positive value drops the oldest queued item FIFO before a new one is appended in add().

Methods

Public · 14

__destruct()

public function __destruct();

Destructor cleanup

Throwing from a destructor is fatal during script shutdown, so an open transaction is auto-committed here (flushing the queued items) rather than throwing.

__serialize()

public function __serialize(): array;

Prevent serialization

__unserialize()

public function __unserialize( array $data ): void;

Prevent unserialization

add()

public function add( Item $item ): AdapterInterface;

Adds a message to the queue

begin()

public function begin(): AdapterInterface;

Starts a transaction

close()

abstract public function close(): bool;

Closes the logger

commit()

public function commit(): AdapterInterface;

Commits the internal transaction

getFormatter()

public function getFormatter(): FormatterInterface;

getQueueLimit()

public function getQueueLimit(): int;

Returns the configured transaction-queue cap (0 = unlimited)

inTransaction()

public function inTransaction(): bool;

Returns the whether the logger is currently in an active transaction or not

process()

abstract public function process( Item $item ): void;

Processes the message in the adapter

rollback()

public function rollback(): AdapterInterface;

Rollbacks the internal transaction

setFormatter()

public function setFormatter( FormatterInterface $formatter ): AdapterInterface;

Sets the message formatter

setQueueLimit()

public function setQueueLimit( int $queueLimit ): AdapterInterface;

Sets the maximum number of items retained in the transaction queue. 0 disables the cap (the default; preserves the original unbounded behavior).

Protected · 1

getFormattedItem()

protected function getFormattedItem( Item $item ): string;

Returns the formatted item

Logger\Adapter\AdapterInterface

Interface Source on GitHub

Phalcon\Logger\AdapterInterface

Interface for Phalcon\Logger adapters

Uses Phalcon\Contracts\Logger\Adapter\Adapter

Logger\Adapter\Exceptions\FileOpenFailed

Class Source on GitHub

Uses Phalcon\Logger\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct(
    string $name,
    string $mode
);

Logger\Adapter\Exceptions\InvalidStreamMode

Class Source on GitHub

Uses Phalcon\Logger\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Logger\Adapter\Exceptions\SyslogOpenFailed

Class Source on GitHub

Uses Phalcon\Logger\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct(
    string $name,
    int $facility
);

Logger\Adapter\Noop

Class Source on GitHub

Class Noop

@package Phalcon\Logger\Adapter

Uses Phalcon\Logger\Item

Method Summary

Methods

Public · 2

close()

public function close(): bool;

Closes the stream

process()

public function process( Item $item ): void;

Processes the message i.e. writes it to the file

Logger\Adapter\Stream

Class Source on GitHub

Phalcon\Logger\Adapter\Stream

Adapter to store logs in plain text files

$logger = new \Phalcon\Logger\Adapter\Stream('app/logs/test.log');

$logger->log('This is a message');
$logger->log(\Phalcon\Logger\Enum::ERROR, 'This is an error');
$logger->error('This is another error');

$logger->close();

@property resource|null $handler @property string $mode @property string $name

Uses Phalcon\Logger\Adapter\Exceptions\FileOpenFailed · Phalcon\Logger\Adapter\Exceptions\InvalidStreamMode · Phalcon\Logger\Item

Method Summary

Properties

protected resource|null $handler = null Stream handler resource
protected string $mode = "ab" The file open mode. Defaults to 'ab'
protected string $name Stream name

Methods

Public · 4

__construct()

public function __construct(
    string $name,
    array $options = []
);

Stream constructor.

close()

public function close(): bool;

Closes the stream

getName()

public function getName(): string;

Stream name

process()

public function process( Item $item ): void;

Processes the message i.e. writes it to the file

Protected · 3

phpFclose()

protected function phpFclose( mixed $handle ): bool;

@todo to be removed when we get traits

phpFopen()

protected function phpFopen(
    string $filename,
    string $mode
);

@todo to be removed when we get traits

phpFwrite()

protected function phpFwrite(
    mixed $handle,
    string $message
);

@todo to be removed when we get traits

Logger\Adapter\Syslog

Class Source on GitHub

Class Syslog

@property string $defaultFormatter @property int $facility @property string $name @property bool $opened @property int $option

Uses Phalcon\Logger\Adapter\Exceptions\SyslogOpenFailed · Phalcon\Logger\Enum · Phalcon\Logger\Item

Method Summary

Properties

protected int $facility = 0
protected string $name = ""
protected bool $opened = false
protected int $option = 0

Methods

Public · 3

__construct()

public function __construct(
    string $name,
    array $options = []
);

Syslog constructor.

close()

public function close(): bool;

Closes the logger

process()

public function process( Item $item ): void;

Processes the message i.e. writes it to the syslog

Protected · 1

openlog()

protected function openlog(
    string $ident,
    int $option,
    int $facility
): bool;

Open connection to system logger

@link https://php.net/manual/en/function.openlog.php

Logger\Enum

Class Source on GitHub

Log Level Enum constants

  • Phalcon\Logger\Enum

Constants

int ALERT = 2
int CRITICAL = 1
int CUSTOM = 8
int DEBUG = 7
int EMERGENCY = 0
int ERROR = 3
int INFO = 6
int NOTICE = 5
int TRACE = 9
int WARNING = 4

Logger\Exception

Class Source on GitHub

Phalcon\Logger\Exception

Exceptions thrown in Phalcon\Logger will use this class

Logger\Exceptions\AdapterNotFound

Class Source on GitHub

Uses Phalcon\Logger\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct( string $name );

Logger\Exceptions\DeserializationFailed

Class Source on GitHub

Uses Phalcon\Logger\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Logger\Exceptions\NoAdaptersConfigured

Class Source on GitHub

Uses Phalcon\Logger\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Logger\Exceptions\SerializationFailed

Class Source on GitHub

Uses Phalcon\Logger\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Logger\Exceptions\TransactionAlreadyActive

Class Source on GitHub

Uses Phalcon\Logger\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Logger\Exceptions\TransactionNotActive

Class Source on GitHub

Uses Phalcon\Logger\Exception

Method Summary

Methods

Public · 1

__construct()

public function __construct();

Logger\Formatter\AbstractFormatter

Abstract Source on GitHub

Class AbstractFormatter

Uses DateTimeImmutable · Phalcon\Logger\Item · Phalcon\Support\Helper\Str\AbstractStr

Method Summary

Properties

protected string $dateFormat = "c" Default date format
protected string $interpolatorLeft = "%"
protected string $interpolatorRight = "%"

Methods

Public · 2

getDateFormat()

public function getDateFormat(): string;

setDateFormat()

public function setDateFormat( string $format ): void;
Protected · 2

getFormattedDate()

protected function getFormattedDate( Item $item ): string;

Returns the date formatted for the logger.

getInterpolatedMessage()

protected function getInterpolatedMessage(
    Item $item,
    string $message
): string;

Logger\Formatter\FormatterInterface

Interface Source on GitHub

Phalcon\Logger\FormatterInterface

This interface must be implemented by formatters in Phalcon\Logger

Uses Phalcon\Contracts\Logger\Formatter\Formatter

Logger\Formatter\Json

Class Source on GitHub

Formats messages using JSON encoding

Uses JsonException · Phalcon\Logger\Item

Method Summary

Methods

Public · 2

__construct()

public function __construct(
    string $dateFormat = "c",
    string $interpolatorLeft = "%",
    string $interpolatorRight = "%"
);

Json constructor.

format()

public function format( Item $item ): string;

Applies a format to a message before sent it to the internal log

Logger\Formatter\Line

Class Source on GitHub

Class Line

Uses Exception · Phalcon\Logger\Item

Method Summary

Properties

protected string $format Format applied to each message

Methods

Public · 4

__construct()

public function __construct(
    string $format = "[%date%][%level%] %message%",
    string $dateFormat = "c",
    string $interpolatorLeft = "%",
    string $interpolatorRight = "%"
);

Line constructor.

format()

public function format( Item $item ): string;

Applies a format to a message before sent it to the internal log

getFormat()

public function getFormat(): string;

Return the format applied to each message

setFormat()

public function setFormat( string $format ): static;

Set the format applied to each message

Logger\Item

Class Source on GitHub

Phalcon\Logger\Item

Represents each item in a logging transaction

@property array $context @property string $message @property int $level @property string $levelName @property DateTimeImmutable $dateTime

  • Phalcon\Logger\Item

Uses DateTimeImmutable

Method Summary

Properties

protected array $context = []
protected DateTimeImmutable $dateTime
protected int $level
protected string $levelName
protected string $message

Methods

Public · 6

__construct()

public function __construct(
    string $message,
    string $levelName,
    int $level,
    DateTimeImmutable $dateTime,
    array $context = []
);

Item constructor.

getContext()

public function getContext(): array;

getDateTime()

public function getDateTime(): DateTimeImmutable;

getLevel()

public function getLevel(): int;

getLevelName()

public function getLevelName(): string;

getMessage()

public function getMessage(): string;

Logger\Logger

Class Source on GitHub

Phalcon Logger.

A logger, with various adapters and formatters. A formatter interface is available as well as an adapter one. Adapters can be created easily using the built-in AdapterFactory. A LoggerFactory is also available that allows developers to create new instances of the Logger or load them from config files (see Phalcon\Config\Config object).

Uses Exception · Phalcon\Logger\Exception

Method Summary

Methods

Public · 10

alert()

public function alert(
    string $message,
    array $context = []
): void;

Action must be taken immediately.

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

critical()

public function critical(
    string $message,
    array $context = []
): void;

Critical conditions.

Example: Application component unavailable, unexpected exception.

debug()

public function debug(
    string $message,
    array $context = []
): void;

Detailed debug information.

emergency()

public function emergency(
    string $message,
    array $context = []
): void;

System is unusable.

error()

public function error(
    string $message,
    array $context = []
): void;

Runtime errors that do not require immediate action but should typically be logged and monitored.

info()

public function info(
    string $message,
    array $context = []
): void;

Interesting events.

Example: User logs in, SQL logs.

log()

public function log(
    mixed $level,
    string $message,
    array $context = []
): void;

Logs with an arbitrary level.

An unknown level (a typo or an unmapped value) is not rejected; it maps to the CUSTOM level and is logged, rather than raising an exception.

notice()

public function notice(
    string $message,
    array $context = []
): void;

Normal but significant events.

trace()

public function trace(
    string $message,
    array $context = []
): void;

Extra-verbose diagnostic output.

Use for high-frequency, fine-grained events such as raw socket frames, HTTP response bodies, or internal state transitions that are too noisy for DEBUG.

warning()

public function warning(
    string $message,
    array $context = []
): void;

Exceptional occurrences that are not errors.

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

Logger\LoggerFactory

Class Source on GitHub

Factory creating logger objects

Uses DateTimeZone · Phalcon\Config\ConfigInterface · Phalcon\Factory\AbstractConfigFactory

Method Summary

Methods

Public · 3

__construct()

public function __construct( AdapterFactory $factory );

load()

public function load( mixed $config ): Logger;

Factory to create an instance from a Config object

newInstance()

public function newInstance(
    string $name,
    array $adapters = [],
    DateTimeZone $timezone = null
): Logger;

Returns a Logger object

Protected · 2

getArrVal()

protected function getArrVal(
    array $collection,
    mixed $index,
    mixed $defaultValue = null
): mixed;

@todo Remove this when we get traits

getExceptionClass()

protected function getExceptionClass(): string;

Logger\LoggerInterface

Interface Source on GitHub

Interface for Phalcon based logger objects.

Uses Phalcon\Contracts\Logger\Logger