RotatingFileWriter extends FileWriter
Write logs into files while providing basic rotation capabilities. This is a very basic approach, suitable for environments where established tools like logrotate are not available.
Table of Contents
Properties
- $defaultLogFileTemplate : string
- Default log file path
- $logFile : string
- Log file path, relative to TYPO3's base project folder
- $logFileHandles : array<string|int, mixed>
- Log file handle storage
- $logFileHandlesCount : array<string|int, mixed>
- Keep track of used file handles by different fileWriter instances
- $logFileInfix : string
Methods
- __construct() : mixed
- Constructor, opens the log file handle
- __destruct() : mixed
- Destructor, closes the log file handle
- __wakeup() : mixed
- Deny object deserialization.
- getLogFile() : string
- Gets the path to the log file.
- setLogFile() : WriterInterface
- Sets the path to the log file.
- setLogFileInfix() : mixed
- writeLog() : WriterInterface
- Writes the log record
- closeLogFile() : mixed
- Closes the log file handle.
- createHtaccessFile() : mixed
- Creates .htaccess file inside a new directory to access protect it
- createLogFile() : mixed
- Creates the log file with correct permissions and parent directories, if needed
- determineNextRotation() : DateTimeImmutable
- formatContextValue() : string
- Escape or quote a value from the context appropriate for the output.
- formatException() : string
- Formats an exception into a string.
- getDefaultLogFileName() : string
- Returns the path to the default log file.
- getLastRotation() : DateTimeImmutable
- interpolate() : string
- Interpolates context values into the message placeholders.
- needsRotation() : bool
- Check if log files need to be rotated under following conditions:
- openLogFile() : mixed
- Opens the log file handle
- rotate() : void
- This method rotates all log files found by using `glob()` to take all already rotated logs into account, even after a configuration change.
- setInterval() : void
- Internal setter called by FileWriter constructor
- setMaxFiles() : void
- Internal setter called by FileWriter constructor
- updateRuntimeRotationState() : void
Properties
$defaultLogFileTemplate
Default log file path
protected
string
$defaultLogFileTemplate
= '/log/typo3_%s.log'
$logFile
Log file path, relative to TYPO3's base project folder
protected
string
$logFile
= ''
$logFileHandles
Log file handle storage
protected
static array<string|int, mixed>
$logFileHandles
= []
To avoid concurrent file handles on a the same file when using several FileWriter instances, we share the file handles in a static class variable
Tags
$logFileHandlesCount
Keep track of used file handles by different fileWriter instances
protected
static array<string|int, mixed>
$logFileHandlesCount
= []
As the logger gets instantiated by class name but the resources are shared via the static $logFileHandles we need to track usage of file handles to avoid closing handles that are still needed by different instances. Only if the count is zero may the file handle be closed.
$logFileInfix
protected
string
$logFileInfix
= ''
Methods
__construct()
Constructor, opens the log file handle
public
__construct([array<string|int, mixed> $options = [] ]) : mixed
Parameters
- $options : array<string|int, mixed> = []
-
Configuration options - depends on the actual log writer
__destruct()
Destructor, closes the log file handle
public
__destruct() : mixed
__wakeup()
Deny object deserialization.
public
__wakeup() : mixed
getLogFile()
Gets the path to the log file.
public
getLogFile() : string
Return values
stringsetLogFile()
Sets the path to the log file.
public
setLogFile(string $relativeLogFile) : WriterInterface
Parameters
- $relativeLogFile : string
-
path to the log file, relative to public web dir
Return values
WriterInterfacesetLogFileInfix()
public
setLogFileInfix(string $infix) : mixed
Parameters
- $infix : string
writeLog()
Writes the log record
public
writeLog(LogRecord $record) : WriterInterface
Parameters
- $record : LogRecord
-
Log record
Return values
WriterInterface —$this
closeLogFile()
Closes the log file handle.
protected
closeLogFile() : mixed
createHtaccessFile()
Creates .htaccess file inside a new directory to access protect it
protected
createHtaccessFile(string $htaccessFile) : mixed
Parameters
- $htaccessFile : string
-
Path of .htaccess file
createLogFile()
Creates the log file with correct permissions and parent directories, if needed
protected
createLogFile() : mixed
determineNextRotation()
protected
determineNextRotation() : DateTimeImmutable
Return values
DateTimeImmutableformatContextValue()
Escape or quote a value from the context appropriate for the output.
protected
formatContextValue(string $value) : string
Note: In some output cases, escaping should not be done here but later on output, such as if it's being written to a database for later display.
Parameters
- $value : string
Return values
stringformatException()
Formats an exception into a string.
protected
formatException(Throwable $ex) : string
The format here is nearly the same as just casting an exception to a string, but omits the full class namespace and stack trace, as those get very long.
Parameters
- $ex : Throwable
Return values
stringgetDefaultLogFileName()
Returns the path to the default log file.
protected
getDefaultLogFileName() : string
Uses the defaultLogFileTemplate and replaces the %s placeholder with a short MD5 hash based on a static string and the current encryption key.
Return values
stringgetLastRotation()
protected
getLastRotation() : DateTimeImmutable
Return values
DateTimeImmutableinterpolate()
Interpolates context values into the message placeholders.
protected
interpolate(string $message[, array<string|int, mixed> $context = [] ]) : string
Parameters
- $message : string
- $context : array<string|int, mixed> = []
Return values
stringneedsRotation()
Check if log files need to be rotated under following conditions:
protected
needsRotation() : bool
- a) either the next rotation is due b) logs were never rotated before
- the log file is not empty - FileWriter::setLogFile() creates one if missing
Return values
boolopenLogFile()
Opens the log file handle
protected
openLogFile() : mixed
Tags
rotate()
This method rotates all log files found by using `glob()` to take all already rotated logs into account, even after a configuration change.
protected
rotate() : void
Log files are rotated using the "copytruncate" approach: the current open log file is copied as-is to a new location, the current log file gets flushed afterward. This way, file handles don't need to get re-created.
setInterval()
Internal setter called by FileWriter constructor
protected
setInterval(string|Interval $interval) : void
Parameters
- $interval : string|Interval
setMaxFiles()
Internal setter called by FileWriter constructor
protected
setMaxFiles(int $maxFiles) : void
Parameters
- $maxFiles : int
updateRuntimeRotationState()
protected
updateRuntimeRotationState(DateTimeImmutable $lastRotation) : void
Parameters
- $lastRotation : DateTimeImmutable