\Omega\Logging Logger

Class Logger.

This class is responsible for handling log operations, including writing log messages to files, managing log levels, formatting messages, and handling log contexts. It supports various log configurations such as log format, date format, log file path, and context management.

Summary

Methods
Properties
Constants
__construct
setLogToStdOut
setLogFilePath
setFileHandle
setDateFormat
setLogLevelThreshold
write
getLogFilePath
getLastLogLine
log
__destruct
emergency
alert
critical
error
warning
notice
info
debug
No public properties found
No public constants found
formatMessage
contextToString
indent
options
logLevelThreshold
logLevels
No protected constants found
getTimestamp
logFilePath
logLineCount
fileHandle
lastLine
defaultPermissions
No private constants found

Properties

$options

$options : array

Log array options.

Type

array<string, mixed> — Contains various configuration options for the logger, such as: - 'extension': The file extension for log files (default: 'txt'). - 'dateFormat': The format for timestamps in the logs (default: 'Y-m-d G:i:s.u'). - 'filename': Custom filename for the log file (default: false). - 'flushFrequency': The number of lines before flushing the output buffer (default: false). - 'prefix': The prefix for the log file (default: 'log_'). - 'logFormat': The format string for log entries (default: false). - 'appendContext': Whether to append context information to log messages (default: true).

$logLevelThreshold

$logLevelThreshold : string

Threshold for log levels.

Type

string — The minimum log level for which messages will be logged (default: LogLevel::DEBUG).

$logLevels

$logLevels : array

Available log levels and their priorities.

Type

array<string, int> — Maps log levels (e.g., 'EMERGENCY', 'DEBUG') to their numeric priorities.

$logFilePath

$logFilePath : string

Type

string

$logLineCount

$logLineCount : int

The number of log lines written so far.

Type

int — Holds the number of log lines written so far.

$fileHandle

$fileHandle : mixed

The file handle for writing log messages.

Type

mixed — Holds the file handle for writing log message.

$lastLine

$lastLine : string

The last line written to the log.

Type

string — Holds the last line written to the log.

$defaultPermissions

$defaultPermissions : int

Default permissions for newly created log directories.

Type

int — Holds the default permission for newly created log directories.

Methods

__construct()

__construct(string  $logDirectory, string  $logLevelThreshold = LogLevel::DEBUG, array  $options = []) : void

Constructor.

Initializes the logger by setting up the log directory, log level, and file options.

Parameters

string $logDirectory

The directory where log files are stored.

string $logLevelThreshold

The log level threshold (default: LogLevel::DEBUG).

array $options

Optional configurations for the logger.

Throws

\RuntimeException

If the log file cannot be created or opened.

Returns

void —

setLogToStdOut()

setLogToStdOut(string  $stdOutPath) : void

Sets the log file to use stdout for logging.

Parameters

string $stdOutPath

The stdout path for logging.

Returns

void —

setLogFilePath()

setLogFilePath(string  $logDirectory) : void

Sets the path to the log file.

Parameters

string $logDirectory

The directory where the log file will be stored.

Returns

void —

setFileHandle()

setFileHandle(string  $writeMode) : void

Opens the log file for writing.

Parameters

string $writeMode

The mode in which to open the log file (e.g., 'a' for append).

Throws

\RuntimeException

If the log file cannot be opened.

Returns

void —

setDateFormat()

setDateFormat(string  $dateFormat) : void

Sets the date format for log entries.

Parameters

string $dateFormat

The date format to use.

Returns

void —

setLogLevelThreshold()

setLogLevelThreshold(string  $logLevelThreshold) : void

Sets the log level threshold.

Parameters

string $logLevelThreshold

The log level threshold to set.

Returns

void —

write()

write(string  $message) : void

Writes a log message to the log file.

Parameters

string $message

The message to log.

Throws

\RuntimeException

If the file cannot be written to.

Returns

void —

getLogFilePath()

getLogFilePath() : string

Get the file path that the log is currently writing to.

Returns

string —

Return the pth of the log file.

getLastLogLine()

getLastLogLine() : string

Get the last line logged to the log file.

Returns

string —

Returns the last line logged to the file.

log()

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

Logs with an arbitrary level.

Parameters

mixed $level

Holds the log level.

string|\Stringable $message

Holds the log message.

array $context

Holds the context of message.

Throws

\Omega\Logging\Exception\LogArgumentException

Returns

void —

__destruct()

__destruct() : void

Destructor.

Closes the file handle when the logger is destroyed.

Returns

void —

emergency()

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

System is unusable.

Parameters

string|\Stringable $message

Holds the message for system is unusable.

array $context

Holds the context of message.

Returns

void —

alert()

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

Action must be taken immediately.

Parameters

string|\Stringable $message

Holds the message for action must be taken immediately.

array $context

Holds the context of message.

Returns

void —

critical()

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

Critical condition.

Parameters

string|\Stringable $message

Holds the message for critical condition.

array $context

Holds the context of message.

Returns

void —

error()

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

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

Parameters

string|\Stringable $message

Holds the message for runtime errors.

array $context

Holds the context of message.

Returns

void —

warning()

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

Exceptional occurrences that are not errors.

Parameters

string|\Stringable $message

Holds the message for exceptional errors.

array $context

Holds the context of message.

Returns

void —

notice()

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

Normal but significant events.

Parameters

string|\Stringable $message

Holds the message for normal but significant events.

array $context

Holds the context of message.

Returns

void —

info()

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

Interesting events.

Parameters

string|\Stringable $message

Holds the message for interesting events.

array $context

Holds the context of message.

Returns

void —

debug()

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

Detailed debug information.

Parameters

string|\Stringable $message

Holds the message for detailed debug information.

array $context

Holds the context of message.

Returns

void —

formatMessage()

formatMessage(string  $level, string|\Stringable  $message, array  $context) : string

Formats a log message for writing to the file.

Parameters

string $level

The log level of the message.

string|\Stringable $message

The message to log.

array $context

The context for the log message.

Returns

string —

The formatted message.

contextToString()

contextToString(array  $context) : string

Converts the given context array to a string.

Parameters

array $context

The context data.

Returns

string —

The context as a string.

indent()

indent(string  $string, string  $indent = '    ') : string

Indents the given string with the given indent.

Parameters

string $string

The string to indent

string $indent

What to use as the indent.

Returns

string —

Return the given string with the given indent.

getTimestamp()

getTimestamp() : string

Gets the correctly formatted Date/Time for the log entry.

PHP DateTime is dump, and you have to resort to trickery to get microseconds to work correctly, so here it is.

Throws

\DateMalformedStringException

Returns

string —

Return the formatted timestamp.