SyslogLog
extends BaseLog
in package
Syslog stream for Logging. Writes logs to the system logger
Table of Contents
Properties
- $_config : array<string, mixed>
- Runtime config
- $_configInitialized : bool
- Whether the config property has already been configured with defaults
- $_defaultConfig : array<string, mixed>
- Default config for this class
- $_levelMap : array<string|int, int>
- Used to map the string names back to their LOG_* constants
- $_open : bool
- Whether the logger connection is open or not
- $formatter : AbstractFormatter
Methods
- __construct() : mixed
- __construct method
- __destruct() : mixed
- Closes the logger connection
- alert() : void
- Action must be taken immediately.
- configShallow() : $this
- Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
- critical() : void
- Critical conditions.
- debug() : void
- Detailed debug information.
- emergency() : void
- System is unusable.
- error() : void
- Runtime errors that do not require immediate action but should typically be logged and monitored.
- getConfig() : mixed
- Returns the config.
- getConfigOrFail() : mixed
- Returns the config for this specific key.
- info() : void
- Interesting events.
- levels() : array<string|int, string>
- Get the levels this logger is interested in.
- log() : void
- Writes a message to syslog
- notice() : void
- Normal but significant events.
- scopes() : array<string|int, string>|false
- Get the scopes this logger is interested in.
- setConfig() : $this
- Sets the config.
- warning() : void
- Exceptional occurrences that are not errors.
- _configDelete() : void
- Deletes a single config key.
- _configRead() : mixed
- Reads a config key.
- _configWrite() : void
- Writes a config key.
- _format() : string
- Formats the message to be logged.
- _open() : void
- Extracts the call to openlog() in order to run unit tests on it. This function will initialize the connection to the system logger
- _write() : bool
- Extracts the call to syslog() in order to run unit tests on it. This function will perform the actual write in the system logger
- interpolate() : string
- Replaces placeholders in message string with context values.
Properties
$_config
Runtime config
protected
array<string, mixed>
$_config
= []
$_configInitialized
Whether the config property has already been configured with defaults
protected
bool
$_configInitialized
= false
$_defaultConfig
Default config for this class
protected
array<string, mixed>
$_defaultConfig
= ['levels' => [], 'scopes' => [], 'flag' => LOG_ODELAY, 'prefix' => '', 'facility' => LOG_USER, 'formatter' => ['className' => \Cake\Log\Formatter\DefaultFormatter::class, 'includeDate' => false]]
By default messages are formatted as: level: message
To override the log format (e.g. to add your own info) define the format key when configuring this logger
If you wish to include a prefix to all messages, for instance to identify the application or the web server, then use the prefix option. Please keep in mind the prefix is shared by all streams using syslog, as it is dependent of the running process. For a local prefix, to be used only by one stream, you can use the format key.
Example:
Log::config('error', ]
'engine' => 'Syslog',
'levels' => ['emergency', 'alert', 'critical', 'error'],
'prefix' => 'Web Server 01'
]);
$_levelMap
Used to map the string names back to their LOG_* constants
protected
array<string|int, int>
$_levelMap
= ['emergency' => LOG_EMERG, 'alert' => LOG_ALERT, 'critical' => LOG_CRIT, 'error' => LOG_ERR, 'warning' => LOG_WARNING, 'notice' => LOG_NOTICE, 'info' => LOG_INFO, 'debug' => LOG_DEBUG]
$_open
Whether the logger connection is open or not
protected
bool
$_open
= false
$formatter
protected
AbstractFormatter
$formatter
Methods
__construct()
__construct method
public
__construct([array<string|int, mixed> $config = [] ]) : mixed
Parameters
- $config : array<string|int, mixed> = []
-
Configuration array
Tags
__destruct()
Closes the logger connection
public
__destruct() : mixed
alert()
Action must be taken immediately.
public
alert(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up.
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
configShallow()
Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
public
configShallow(array<string, mixed>|string $key[, mixed|null $value = null ]) : $this
Setting a specific value:
$this->configShallow('key', $value);
Setting a nested value:
$this->configShallow('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
Return values
$thiscritical()
Critical conditions.
public
critical(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Example: Application component unavailable, unexpected exception.
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
debug()
Detailed debug information.
public
debug(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
emergency()
System is unusable.
public
emergency(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
error()
Runtime errors that do not require immediate action but should typically be logged and monitored.
public
error(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
getConfig()
Returns the config.
public
getConfig([string|null $key = null ][, mixed $default = null ]) : mixed
Usage
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key');
Reading a nested value:
$this->getConfig('some.nested.key');
Reading with default value:
$this->getConfig('some-key', 'default-value');
Parameters
- $key : string|null = null
-
The key to get or null for the whole config.
- $default : mixed = null
-
The return value when the key does not exist.
Return values
mixed —Configuration data at the named key or null if the key does not exist.
getConfigOrFail()
Returns the config for this specific key.
public
getConfigOrFail(string $key) : mixed
The config value for this key must exist, it can never be null.
Parameters
- $key : string
-
The key to get.
Tags
Return values
mixed —Configuration data at the named key
info()
Interesting events.
public
info(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Example: User logs in, SQL logs.
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
levels()
Get the levels this logger is interested in.
public
levels() : array<string|int, string>
Return values
array<string|int, string>log()
Writes a message to syslog
public
log(mixed $level, string $message[, array<string|int, mixed> $context = [] ]) : void
Map the $level back to a LOG_ constant value, split multi-line messages into multiple log messages, pass all messages through the format defined in the configuration
Parameters
- $level : mixed
-
The severity level of log you are making.
- $message : string
-
The message you want to log.
- $context : array<string|int, mixed> = []
-
Additional information about the logged message
Tags
notice()
Normal but significant events.
public
notice(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
scopes()
Get the scopes this logger is interested in.
public
scopes() : array<string|int, string>|false
Return values
array<string|int, string>|falsesetConfig()
Sets the config.
public
setConfig(array<string, mixed>|string $key[, mixed|null $value = null ][, bool $merge = true ]) : $this
Usage
Setting a specific value:
$this->setConfig('key', $value);
Setting a nested value:
$this->setConfig('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
- $merge : bool = true
-
Whether to recursively merge or overwrite existing config, defaults to true.
Tags
Return values
$thiswarning()
Exceptional occurrences that are not errors.
public
warning(string|Stringable $message[, array<string|int, mixed> $context = [] ]) : void
Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong.
Parameters
- $message : string|Stringable
- $context : array<string|int, mixed> = []
_configDelete()
Deletes a single config key.
protected
_configDelete(string $key) : void
Parameters
- $key : string
-
Key to delete.
Tags
_configRead()
Reads a config key.
protected
_configRead(string|null $key) : mixed
Parameters
- $key : string|null
-
Key to read.
_configWrite()
Writes a config key.
protected
_configWrite(array<string, mixed>|string $key, mixed $value[, string|bool $merge = false ]) : void
Parameters
- $key : array<string, mixed>|string
-
Key to write to.
- $value : mixed
-
Value to write.
- $merge : string|bool = false
-
True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Tags
_format()
Formats the message to be logged.
protected
_format(string $message[, array<string|int, mixed> $context = [] ]) : string
Call interpolate()
directly from your log engine and format the message in a formatter.
The context can optionally be used by log engines to interpolate variables or add additional info to the logged message.
Parameters
- $message : string
-
The message to be formatted.
- $context : array<string|int, mixed> = []
-
Additional logging information for the message.
Return values
string_open()
Extracts the call to openlog() in order to run unit tests on it. This function will initialize the connection to the system logger
protected
_open(string $ident, int $options, int $facility) : void
Parameters
- $ident : string
-
the prefix to add to all messages logged
- $options : int
-
the options flags to be used for logged messages
- $facility : int
-
the stream or facility to log to
_write()
Extracts the call to syslog() in order to run unit tests on it. This function will perform the actual write in the system logger
protected
_write(int $priority, string $message) : bool
Parameters
- $priority : int
-
Message priority.
- $message : string
-
Message to log.
Return values
boolinterpolate()
Replaces placeholders in message string with context values.
protected
interpolate(string $message[, array<string|int, mixed> $context = [] ]) : string
Parameters
- $message : string
-
Formatted string
- $context : array<string|int, mixed> = []
-
Context for placeholder values.