PHP-Helpers

Logger
in package

Logger class that handles writing out logfiles for PHP programs.

Possible loglevels are:

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

A regular logging entry (in the combined logfile) has the format: Y-m-d H:i:s.u [loglevel] [class_calling] [length]: message

(See PHP documentation for date format; LOG_MICROSEC=false will remove .u from date-time.)

Within the loglevel specific files, the [loglevel] entry is removed.

[length] represents the length of the logmessage message (in Bytes); date, time and the square braced parts are not reflected.

Possible environmental variables – defaults are defined in static::$localDefaults: | environmental variable | description | | ---------------------- | ----------- | | LOG_PATH | where should the log files be placed? Defaults to /tmp/logs/ | | LOG_LEVEL | one of the loglevels, defaults to ERROR | | LOG_MICROSEC | should microseconds be reflected? Default is true | | LOG_TIMEZONE | the timezone the logs should reflect, defaults to environmental variable TIMEZONE if it exists or Europe/Berlin | | LOG_COMBINED | if true, additionally a full.log file will be created | | LOG_ONLY_COMBINED | if true and LOG_COMBINED also true, only the full.log file is written out – else, there will also be loglevel.log files |

Table of Contents

$availableLoglevels  : array<string|int, mixed>
ordered (!) list of available LogLevels
$localDefaults  : array<string|int, mixed>
dictionary of defaults for used environmental variables
$clss  : mixed
$envs  : mixed
$filenames  : array<string|int, mixed>
dictionary of additional filenames
$lvl  : mixed
$msg  : mixed
$spec  : mixed
$time  : mixed
__callStatic()  : mixed
function that handles loglevel method calls to this static class and returns an `\Exception` on all unkown static method calls on this class
__toString()  : string
create string representation of current log entry
ensureLogPathExists()  : bool
Ensure that the log path does exist
getFilename()  : string
get filename for logfiles
getLogStringFormat()  : string
return the format string for a log message
__construct()  : mixed
initialize the logger
get_calling_class()  : string
get the name of the calling class for logging
appendToFile()  : mixed
function that performs the write out
writeOut()  : void
function that arranges the writeout to the logfile

Properties

$availableLoglevels

ordered (!) list of available LogLevels

protected static array<string|int, mixed> $availableLoglevels = ['DEBUG', 'INFO', 'NOTICE', 'WARNING', 'ERROR', 'CRITICAL', 'ALERT', 'EMERGENCY']

$localDefaults

dictionary of defaults for used environmental variables

protected static array<string|int, mixed> $localDefaults = ['LOG_PATH' => '/tmp/logs/', 'LOG_LEVEL' => 'error', 'LOG_MICROSEC' => true, 'LOG_TIMEZONE' => 'Europe/Berlin', 'LOG_COMBINED' => true, 'LOG_ONLY_COMBINED' => true]

$clss

private mixed $clss = NULL

$filenames

dictionary of additional filenames

private static array<string|int, mixed> $filenames = ['full' => 'full.log']

$spec

private mixed $spec = false

$time

private mixed $time = NULL

Methods

__callStatic()

function that handles loglevel method calls to this static class and returns an `\Exception` on all unkown static method calls on this class

public static __callStatic(string $name, mixed $args) : mixed
Parameters
$name : string

name of the unknown callable

$args : mixed

arguments passed for the callable:

message

return – should the (loglevel) message be returned?

Tags
throws
Exception

Fatal undefined method when callable is not equal to defined loglevel

Return values
mixed

result of the callable, if there is a matching loglevel

__toString()

create string representation of current log entry

public __toString() : string
Return values
string

log entry

ensureLogPathExists()

Ensure that the log path does exist

public static ensureLogPathExists() : bool
Return values
bool

Returns true on success or false on failure.

getFilename()

get filename for logfiles

public static getFilename(string $file) : string
Parameters
$file : string

logfile

Tags
throws
Exception

if the requested logfile type does not exist

Return values
string

filename for logfile

getLogStringFormat()

return the format string for a log message

public static getLogStringFormat([bool $logSpecific = true ]) : string
Parameters
$logSpecific : bool = true

is the log message used in a specific loglevel context, e.g. the specific loglevel log file (true) or not, e.g. the global log file

Return values
string

log message format

__construct()

initialize the logger

protected __construct(string $loglevel, string $message, mixed $class) : mixed
Parameters
$loglevel : string

loglevel out of static::$availableLoglevels

$message : string

the message to be logged

$class : mixed
Tags
throws
Exception

thrown when loglevel does not exist

Return values
mixed

get_calling_class()

get the name of the calling class for logging

protected static get_calling_class() : string
Return values
string

namespace and class name of calling class

appendToFile()

function that performs the write out

private appendToFile(string $name, string $content) : mixed
Parameters
$name : string

filename

$content : string

content to append to the logfile

Return values
mixed

Returns the same like file_put_contents: This function returns the number of bytes that were written to the file, or false on failure.

writeOut()

function that arranges the writeout to the logfile

private writeOut() : void
Return values
void

        

Search results