Oihana PHP System

LoggerManager

AbstractYes

Abstract logger manager class.

Provides utility methods for managing log files, including:

  • Creating a logger instance.
  • Reading log lines.
  • Counting log lines.
  • Clearing log files.
  • Retrieving log file paths and names.

This class is meant to be extended with a concrete implementation of createLogger().

Table of Contents

Constants

DEFAULT_EXTENSION  = '.log'
Default log file extension.
DEFAULT_NAME  = 'log'
Default log file name.
DEFAULT_PATH  = 'log'
Default log folder path (relative to $directory).

Properties

$directory  : string
The base directory for log storage.
$extension  : string
The file extension used for log files (e.g., ".log").
$name  : string|null
Optional name of the logging channel.
$path  : string
Subfolder or path where log files are stored (relative to $directory).

Methods

__construct()  : mixed
Constructor.
clear()  : bool
Clears the content of a specific log file.
countLines()  : int
Returns the number of lines in a log file.
createLog()  : Log|null
Parses a log line into a structured array.
createLogger()  : LoggerInterface
Must be implemented by subclasses to return a PSR-3 compliant logger.
getDirectory()  : string
Returns the full path of the log directory.
getExtension()  : string
Returns the file extension used for log files.
getFileName()  : string
Returns the base name of the log file.
getFilePath()  : string
Returns the full path to a log file.
getLoggerFiles()  : array<string|int, mixed>|false
Returns the list of log files in the log directory matching the current logger name and extension.
getLogLines()  : array<string|int, mixed>|null
Returns the lines of a log file as an array of structured entries.

Constants

DEFAULT_EXTENSION

Default log file extension.

public mixed DEFAULT_EXTENSION = '.log'

DEFAULT_NAME

Default log file name.

public mixed DEFAULT_NAME = 'log'

DEFAULT_PATH

Default log folder path (relative to $directory).

public mixed DEFAULT_PATH = 'log'

Properties

$directory

The base directory for log storage.

public string $directory = \oihana\enums\Char::EMPTY

$extension

The file extension used for log files (e.g., ".log").

public string $extension

$name

Optional name of the logging channel.

public string|null $name

Used as prefix for log files and as a descriptive label.

$path

Subfolder or path where log files are stored (relative to $directory).

public string $path

Methods

__construct()

Constructor.

public __construct([string $directory = Char::EMPTY ][, array<string|int, mixed> $init = [] ][, string|null $name = null ]) : mixed
Parameters
$directory : string = Char::EMPTY

Base directory for log storage.

$init : array<string|int, mixed> = []

Optional initialization options:

  • LoggerConfig::PATH: subdirectory path
  • LoggerConfig::EXTENSION: log file extension
$name : string|null = null

Optional logger channel name.

clear()

Clears the content of a specific log file.

public clear(string $file) : bool
Parameters
$file : string

Log file name.

Tags
throws
FileException

If clearing fails due to filesystem permissions or errors.

Return values
bool

True if the file was cleared, false if file does not exist.

countLines()

Returns the number of lines in a log file.

public countLines(string $file) : int
Parameters
$file : string

Log file name.

Tags
throws
FileException

If file cannot be read.

Return values
int

Number of lines in the file.

createLog()

Parses a log line into a structured array.

public createLog(string $line) : Log|null

Example: "2025-08-21 10:30:00 INFO Some message" becomes: [ 'date' => '2025-08-21', 'time' => '10:30:00', 'level' => 'INFO', 'message' => 'Some message' ]

Parameters
$line : string

Raw log line.

Return values
Log|null

Parsed log entry or null if line is empty or malformed.

createLogger()

Must be implemented by subclasses to return a PSR-3 compliant logger.

public abstract createLogger() : LoggerInterface
Return values
LoggerInterface

getDirectory()

Returns the full path of the log directory.

public getDirectory() : string
Return values
string

Absolute path to the log folder.

getExtension()

Returns the file extension used for log files.

public getExtension() : string
Return values
string

Log file extension, including dot (e.g., ".log").

getFileName()

Returns the base name of the log file.

public getFileName() : string
Return values
string

Log file name.

getFilePath()

Returns the full path to a log file.

public getFilePath([string|null $file = null ]) : string
Parameters
$file : string|null = null

Optional custom file name. Defaults to .

Return values
string

Full path to the log file.

getLoggerFiles()

Returns the list of log files in the log directory matching the current logger name and extension.

public getLoggerFiles() : array<string|int, mixed>|false
Tags
throws
DirectoryException

If the log directory cannot be read.

Return values
array<string|int, mixed>|false

List of log file names or false on error.

getLogLines()

Returns the lines of a log file as an array of structured entries.

public getLogLines(string|null $file) : array<string|int, mixed>|null
Parameters
$file : string|null

Log file name.

Tags
throws
FileException

If the file cannot be read.

Return values
array<string|int, mixed>|null

Array of parsed log entries or null if file does not exist.


        
On this page

Search results