Cascade-Guest: "Cascade WiFi 2008."
Software Developer at Cascade Energy, Inc.
@nackjicholsonn
github:nackjicholson
willieviseoae@gmail.com
will.vaughn@cascadeenergy.com
Symfony2, Silex, AngularJs
Amazon Web Services, NodeJS
You should really just be using monolog
"require": {
"monolog/monolog": "1.x"
}
composer.json
$ composer require "monolog/monolog"="1.x"
composer cli
debug
info
notice
warning
error
critical
alert
emergency
Detailed debug information
Interesting events
Normal but significant events
Exceptional occurrences, not errors
Expected runtime errors
Critical conditions, in need of action
Immediate action is necessary
Catastrophic failure, system unusable
100 -- Logger::DEBUG
200 -- Logger::INFO
250 -- Logger::NOTICE
300 -- Logger::WARNING
400 -- Logger::ERROR
500 -- Logger::CRITICAL
550 -- Logger::ALERT
600 -- Logger::EMERGENCY
Monolog levels:
<?php
use Monolog\Logger;
require 'vendor/autoload.php';
$logger = new Logger('channel');
$logger->debug('debug test');
$logger->warning('warning test');
[2014-11-01 21:46:40] channel.DEBUG: debug test [] []
[2014-11-01 21:46:40] channel.WARNING: warning test [] []
Monolog provides A TON of built in Handlers.
and many more...
Log to multiple destinations.
$filePath = __DIR__ . '/multiple.streams.log';
// Default takes everything >= Logger::DEBUG
$stdoutHandler = new StreamHandler('php://stdout');
// File only handles records with Level >= Logger::WARNING
$fileHandler = new StreamHandler($filePath, Logger::WARNING);
$logger = new Logger('channel');
$logger->pushHandler($stdoutHandler);
$logger->pushHandler($fileHandler);
$logger->info('stdout only');
$logger->warning('stdout and file');
$logger->debug('stdout only');
$logger->error('stdout and file');
# Logged to file
[2014-11-02 01:26:37] channel.WARNING: stdout and file [] []
[2014-11-02 01:26:37] channel.ERROR: stdout and file []
# Logged to stdout
[2014-11-02 01:26:37] channel.INFO: stdout only [] []
[2014-11-02 01:26:37] channel.WARNING: stdout and file [] []
[2014-11-02 01:26:37] channel.DEBUG: stdout only [] []
[2014-11-02 01:26:37] channel.ERROR: stdout and file []
Don't bother me unless theres a real problem.
$handler = new StreamHandler('php://stdout');
$fingersCrossed = new FingersCrossedHandler($handler, Logger::CRITICAL);
$logger = new Logger("channel");
$logger->pushHandler($fingersCrossed);
// These aren't handled until a log of level >= Logger::CRITICAL
$logger->info('info msg');
$logger->notice('notice msg');
// All sent with this one
$logger->critical('critical msg');
# Logged to stdout as a batch
[2014-11-02 01:26:37] channel.INFO: info msg [] []
[2014-11-02 01:26:37] channel.NOTICE: notice msg [] []
[2014-11-02 01:26:37] channel.CRITICAL: critical msg [] []
Send it all at the end with BufferHandler.
// Default to only handling levels >= Logger::CRITICAL
$gitterHandler = new GitterImHandler('apitoken', 'roomId');
$bufferHandler = new BufferHandler($gitterHandler);
$logger = new Logger("channel");
$logger->pushHandler($bufferHandler);
// Buffered, and handled as batch at the end.
$logger->debug('test.debug ignored.');
$logger->critical('test.critical in gitter.im');
$logger->notice('test.notice');
$logger->emergency('test.emergency in gitter.im');
$logger->error('test.error');
Trait for classes which have Logger as a dependency.
trait LoggerAwareTrait
{
/** @var LoggerInterface */
protected $logger;
/**
* Sets a logger.
*
* @param LoggerInterface $logger
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
}
$record = array(
'message' => string,
'context' => array
'level' => integer,
'level_name' => string,
'channel' => string,
'datetime' => float (unix microtime)
'extra' => array
);
The data currency of monolog.
"channel-environment-system"
// Too generic.
$logger = new Logger('foobar');
$logger = new Logger('myWebsite');
// Awesomely specific.
$logger = new Logger('cascade-staging-www');
$logger = new Logger('cascade-production-www');
$logger = new Logger('cascade-development-weather');
$logger = new Logger('otherCompany-development-weather');
$logger->debug('my message', ['foo' => 'bar']);
[2014-11-02 01:26:37] channel.DEBUG: my message {"foo":"bar"} []
$user = new User("will");
$db->put($user);
$logger->info('User ' . $user->username . ' saved.');
[2014-11-02 01:26:37] channel.INFO: User will saved. [] []
$user = new User("will");
$db->put($user);
$context = get_object_vars($user);
$logger->info("User saved.", $context);
[2014-11-02 01:26:37] channel.INFO: User saved. {"username":"will"} []
JsonFormatter
public function format(array $record)
{
return json_encode($record) . ($this->appendNewline ? "\n" : '');
}class MemoryUsageProcessor extends MemoryProcessor
{
public function __invoke(array $record)
{
$bytes = memory_get_usage($this->realUsage);
$formatted = $this->formatBytes($bytes);
$record['extra'] = array_merge(
$record['extra'],
array(
'memory_usage' => $formatted,
)
);
return $record;
}
}