Message logging with observer-based log writing.
This class does not support extensions, only additional writers.
Class declared in SYSPATH/classes/log.php on line 3.
integer 0
integer 1
integer 2
integer 3
integer 4
integer 5
integer 6
integer 7
integer 8
string
$timestamplink to thistimestamp format for log entries
string(11) "Y-m-d H:i:s"
string
$timezonelink to thistimezone for log entries
NULL
boolean
$write_on_addlink to thisimmediately write when logs are added
bool FALSE
Log
$_instancelink to thisSingleton instance container
object Log()
array
$_messageslink to thislist of added messages
array
$_writerslink to thislist of log writers
Adds a message to the log. Replacement values must be passed in to be replaced using strtr.
$log
->add(Log::ERROR,
'Could not locate user: :user'
,
array
(
':user'
=>
$username
,
));
string
$level
required - Level of messagestring
$message
required - Message bodyarray
$values
= NULL - Values to replace in the messageLog
public
function
add(
$level
,
$message
,
array
$values
= NULL)
{
if
(
$values
)
{
// Insert the values into the message
$message
=
strtr
(
$message
,
$values
);
}
// Create a new message and timestamp it
$this
->_messages[] =
array
(
'time'
=>
Date
::formatted_time(
'now'
, Log::
$timestamp
, Log::
$timezone
),
'level'
=>
$level
,
'body'
=>
$message
,
);
if
(Log::
$write_on_add
)
{
// Write logs as they are added
$this
->write();
}
return
$this
;
}
Attaches a log writer, and optionally limits the levels of messages that will be written by the writer.
$log
->attach(
$writer
);
Log_Writer
$writer
required - Instancemixed
$levels
= array(0) - Array of messages levels to write OR max level to writeinteger
$min_level
= integer 0 - Min level to write IF $levels is not an arrayLog
public
function
attach(Log_Writer
$writer
,
$levels
=
array
(),
$min_level
= 0)
{
if
( !
is_array
(
$levels
))
{
$levels
= range(
$min_level
,
$levels
);
}
$this
->_writers[
"{$writer}"
] =
array
(
'object'
=>
$writer
,
'levels'
=>
$levels
);
return
$this
;
}
Detaches a log writer. The same writer object must be used.
$log
->detach(
$writer
);
Log_Writer
$writer
required - InstanceLog
public
function
detach(Log_Writer
$writer
)
{
// Remove the writer
unset(
$this
->_writers[
"{$writer}"
]);
return
$this
;
}
Get the singleton instance of this class and enable writing at shutdown.
$log
= Log::instance();
Log
public
static
function
instance()
{
if
(Log::
$_instance
=== NULL)
{
// Create a new instance
Log::
$_instance
=
new
Log;
// Write the logs at shutdown
register_shutdown_function(
array
(Log::
$_instance
,
'write'
));
}
return
Log::
$_instance
;
}
Write and clear all of the messages.
$log
->write();
void
public
function
write()
{
if
(
empty
(
$this
->_messages))
{
// There is nothing to write, move along
return
;
}
// Import all messages locally
$messages
=
$this
->_messages;
// Reset the messages array
$this
->_messages =
array
();
foreach
(
$this
->_writers
as
$writer
)
{
if
(
empty
(
$writer
[
'levels'
]))
{
// Write all of the messages
$writer
[
'object'
]->write(
$messages
);
}
else
{
// Filtered messages
$filtered
=
array
();
foreach
(
$messages
as
$message
)
{
if
(in_array(
$message
[
'level'
],
$writer
[
'levels'
]))
{
// Writer accepts this kind of message
$filtered
[] =
$message
;
}
}
// Write the filtered messages
$writer
[
'object'
]->write(
$filtered
);
}
}
}