Class FileLogger - Stefanius67/XLogger GitHub Wiki

Full name:     \SKien\XLogger\FileLogger
Parent class:    \SKien\XLogger\XLogger


PSR-3 compliant logger for Output to plain text file (.log, .csv, .txt)

Each log item is represented by one single line containing the fields separated by defined character.

Dependent on the file extension following field separator is used:

  • .log : TAB
  • .csv : semicolon (;)
  • .txt : colon (,)

CR/LF in message are replaced with Space to keep one item in a single line. Field separator is replaced to keep the number of fields consistent.

Overview

Method Description
__construct Init logging level and remote username (if set).
__destruct close file if already opened.
alert Action must be taken immediately.
critical Critical conditions.
debug Detailed debug information.
emergency System is unusable.
error Runtime errors that do not require immediate action but should typically be logged and monitored.
getFilename Get current filename (may be some placeholders are supplemented).
getFullpath Get full path of the logfile.
info Interesting events.
log Logs with an arbitrary level.
notice Normal but significant events.
reset close file if open, reopen and truncate existing file
setFilename Set the filename for the logfile.
setFullpath Set the path and filename for the logfile.
setLogLevel Set the level from witch items should be logged.
setOptions Set Options for logging.
setPath Set the path for the logfile.
setUser Set the username.
warning Exceptional occurrences that are not errors.

Methods

__construct

Init logging level and remote username (if set).

public FileLogger::__construct(string $level = LogLevel::DEBUG)

Parameters:

Parameter Type Description
level string the min. LogLevel to be logged

See Also:

[go to top]


__destruct

close file if already opened.

public FileLogger::__destruct()

[go to top]


log

Logs with an arbitrary level.

public FileLogger::log(string $level, mixed $message, array $context = array()) : void

Parameters:

Parameter Type Description
level string
message mixed
context array

[go to top]


reset

close file if open, reopen and truncate existing file

public FileLogger::reset() : void

[go to top]


Inherited methods

alert

Action must be taken immediately.

public AbstractLogger::alert(string $message, array $context = array()) : void

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

Parameters:

Parameter Type Description
message string
context array

[go to top]


critical

Critical conditions.

public AbstractLogger::critical(string $message, array $context = array()) : void

Example: Application component unavailable, unexpected exception.

Parameters:

Parameter Type Description
message string
context array

[go to top]


debug

Detailed debug information.

public AbstractLogger::debug(string $message, array $context = array()) : void

Parameters:

Parameter Type Description
message string
context array

[go to top]


emergency

System is unusable.

public AbstractLogger::emergency(string $message, array $context = array()) : void

Parameters:

Parameter Type Description
message string
context array

[go to top]


error

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

public AbstractLogger::error(string $message, array $context = array()) : void

Parameters:

Parameter Type Description
message string
context array

[go to top]


getFilename

Get current filename (may be some placeholders are supplemented).

public XLogger::getFilename() : string

Return Type: string

See Also:

[go to top]


getFullpath

Get full path of the logfile.

public XLogger::getFullpath() : string

Return Type: string

See Also:

[go to top]


info

Interesting events.

public AbstractLogger::info(string $message, array $context = array()) : void

Example: User logs in, SQL logs.

Parameters:

Parameter Type Description
message string
context array

[go to top]


notice

Normal but significant events.

public AbstractLogger::notice(string $message, array $context = array()) : void

Parameters:

Parameter Type Description
message string
context array

[go to top]


setFilename

Set the filename for the logfile.

public XLogger::setFilename(string $strFilename) : void

Some placeholders can be used for date/month/year/week.

Parameters:

Parameter Type Description
strFilename string

See Also:

[go to top]


setFullpath

Set the path and filename for the logfile.

public XLogger::setFullpath(string $strFullpath) : void

In order to be able to assign logfiles chronologically and / or to a user, it is possible to use placeholders in the file name or path, which are replaced accordingly before the file is created. Following placeholders are supported:

  • {date} : will be replaced by current date (Format 'YYYY-MM-DD')
  • {month} : will be replaced by current month (Format 'YYYY-MM')
  • {year} : will be replaced by current year (Format 'YYYY')
  • {week} : will be replaced by current ISO-8601 week (Format 'YYYY_WW')
  • {name} : will be replaced by the username

Note:
If you use the placeholder for the user name, this have to be set BEFORE the call of this method. In the username, all characters except A-Z, a-z, 0-9, '_' and '-' are filtered out (to always get a valid file name)!

Parameters:

Parameter Type Description
strFullpath string

[go to top]


setLogLevel

Set the level from witch items should be logged.

public XLogger::setLogLevel(string $strLogLevel) : void

All entries with a lower level than the specified are ignored.
The relevance of the defined PSR-3 level from hight to low are:

  • EMERGENCY
  • ALERT
  • CRITICAL
  • ERROR
  • WARNING
  • NOTICE
  • INFO
  • DEBUG

Parameters:

Parameter Type Description
strLogLevel string the min. LogLevel to be logged

[go to top]


setOptions

Set Options for logging.

public XLogger::setOptions(int $iOptions) : void

Use any combination of:

  • XLogger::LOG_IP : include IP-adress in log item
  • XLogger::LOG_BT : include backtrace 'filename (line)' in log item
  • XLogger::LOG_USER : include user in log item
  • XLogger::LOG_UA : include user-agent in log item

Parameters:

Parameter Type Description
iOptions int any combination (bitwise or, '|') of the flags described

[go to top]


setPath

Set the path for the logfile.

public XLogger::setPath(string $strPath) : void

Some placeholders can be used for date/month/year/week.

Parameters:

Parameter Type Description
strPath string

See Also:

[go to top]


setUser

Set the username.

public XLogger::setUser(string $strUser) : void

Parameters:

Parameter Type Description
strUser string

[go to top]


warning

Exceptional occurrences that are not errors.

public AbstractLogger::warning(string $message, array $context = array()) : void

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

Parameters:

Parameter Type Description
message string
context array

[go to top]


⚠️ **GitHub.com Fallback** ⚠️