the art of application logging phpnw12
DESCRIPTION
Slides from my talk 'The Art Of Application Logging' given at PHPLon and PHPNW12.TRANSCRIPT
![Page 1: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/1.jpg)
The Art Of Application Logging
PHPNW 2012
Ben Waine - @bwaine
http://github.com/benwaine/ApplicationLoggingTalk
![Page 2: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/2.jpg)
Kicking Ass And Logging Names
PHPNW 2012
Ben Waine - @bwaine
http://github.com/benwaine/ApplicationLoggingTalk
![Page 3: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/3.jpg)
Ben Waine
Contractor / Co Founder Recensus
PHP Developer
Logs in spare time....
![Page 4: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/4.jpg)
![Page 5: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/5.jpg)
![Page 6: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/6.jpg)
Roadmap
• Introduction - Why Is Logging Good?
• The Mechanics - Code / Examples / Live Demos (GULP)
• Logging Strategy - What to do with your logs
• Conclusion
![Page 7: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/7.jpg)
Roadmap
• Introduction - Why Is Logging Good?
• The Mechanics - Code / Examples / Live Demos (GULP)
• Logging Strategy - What to do with your logs
• Conclusion
![Page 8: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/8.jpg)
Name Some Logs.....
![Page 9: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/9.jpg)
Name Some Logs.....
![Page 10: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/10.jpg)
The Benefits Logs Provide
![Page 11: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/11.jpg)
tail -f
![Page 12: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/12.jpg)
• Insight into how a process is being executed
• Ability to analyse execution after the fact
• Ability to catch information about execution errors
• Look for patterns and trends
![Page 13: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/13.jpg)
These are all services......
![Page 14: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/14.jpg)
What about application logging?
![Page 15: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/15.jpg)
I
SKYBET’s Log
![Page 16: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/16.jpg)
![Page 17: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/17.jpg)
![Page 18: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/18.jpg)
![Page 19: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/19.jpg)
![Page 20: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/20.jpg)
• Provide direction when picking up an application for the first time.
• Well sign posted layers increases understanding of the request.
• Quick error isolation and context
• Times in logs keep performance prominent
Benefits For Beginners
![Page 21: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/21.jpg)
Benefits For Everyone
![Page 22: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/22.jpg)
![Page 23: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/23.jpg)
Roadmap
• Introduction - Why Is Logging Good?
• The Mechanics - Code / Examples / Live Demos (GULP)
• Logging Strategy - What to do with your logs
• Conclusion
![Page 24: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/24.jpg)
Our Logging GoalsLog information about a script which creates users.
User - Represents a user UserMapper - A class which saves usersScripts - Control User and UserMapper
![Page 25: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/25.jpg)
1) The Basics
https://github.com/benwaine/ApplicationLoggingTalk/tree/master/examples/01-NativeLogging
![Page 26: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/26.jpg)
error_log()
![Page 27: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/27.jpg)
require_once __DIR__ . '/app/User.php';require_once __DIR__ . '/app/UserMapper.php';require_once __DIR__ . '/../../vendor/autoload.php';
// Create a mapper$mapper = new UserMapper();
// Create Users and persist themerror_log('Beginning User Creation');
while(true){ $user = new User(rand(1, 10000), 'Betty Boo');
$mapper->save($user);
sleep(1);
unset($user);}
01-basic.php
![Page 28: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/28.jpg)
UserMapper.php
class UserMapper {
public function save(User $user) {
error_log('Saving User: ' . $user->getId());
// Code For Saving User
}
}
![Page 29: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/29.jpg)
DEMO
![Page 30: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/30.jpg)
02-ToFile.php
require_once __DIR__ . '/app/User.php';require_once __DIR__ . '/app/UserMapper.php';require_once __DIR__ . '/../../vendor/autoload.php';
ini_set('error_log', '/tmp/user.log');
// Create a mapper$mapper = new UserMapper();
// Create Users and persist themerror_log('Beginning User Creation');
while(true){ $user = new User(rand(1, 10000), 'Betty Boo');
$mapper->save($user);
sleep(1);
unset($user);}
![Page 31: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/31.jpg)
• Native Functionality
• error_log, semantically correct?
• PHP errors mixed in with application logging
• Not very powerful compared to other tools
Pros
Cons
![Page 32: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/32.jpg)
2) Logging Library
https://github.com/benwaine/ApplicationLoggingTalk/tree/master/examples/02-LoggingAsADependency
![Page 33: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/33.jpg)
Log4PHP
Monolog
ZendLog
PEARLog
KLogger
![Page 34: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/34.jpg)
Monolog
https://github.com/Seldaek/monolog
![Page 35: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/35.jpg)
Logger
HandlerStreamHandler
HandlerFirePHPHandler
use Monolog\Logger;use Monolog\Handler\StreamHandler;use Monolog\Handler\FirePHPHandler;
// Create the logger$logger = new Logger('my_logger');// Now add some handlers$logger->pushHandler(new StreamHandler(__DIR__.'/my_app.log', Logger::DEBUG));$logger->pushHandler(new FirePHPHandler());
// You can now use your logger$logger->addInfo('My logger is now ready');
Logger delegates to one or more handlers
Handlers are placed in a stack
Formatter(optional)
Monolog Architecture
Log formatting is changed using supplied or custom
formatters.
Handlers can be used by multiple loggers. Formatters can be used by multiple handlers.
![Page 36: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/36.jpg)
Fingers Crossed Logger
![Page 37: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/37.jpg)
01-basic.php
// ... Require Statements ...use Monolog\Logger;use Monolog\Handler\StreamHandler;
// Create a New MonoLog$logger = new Monolog\Logger('Application Log');// Add a handler (writer)$logger->pushHandler(new StreamHandler('/tmp/user.log', Logger::DEBUG));
// Create a mapper$mapper = new UserMapper($logger);
// Create Users and persist them$logger->notice('Beginning User Creation');
while(true){ $user = new User(rand(1, 10000), 'Betty Boo'); $mapper->save($user); sleep(1); unset($user);}
![Page 38: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/38.jpg)
UserMapper.php
use Monolog\Logger;
class UserMapper {
protected $logger;
public function __construct(Logger $logger) { $this->logger = $logger; }
public function save(User $user) {
$this->logger->info('Saving User: ' . $user->getID());
// Code For Saving User
}
}
![Page 39: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/39.jpg)
DEMO
![Page 40: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/40.jpg)
• Object Oriented Logging
• Multiple Log Writers (File, Email, Firebug, StdOut)
• Log Levels (See Example)
• ‘Unwieldy’ instantiation
• Logging control code permeates application layer
Pros
Cons
![Page 41: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/41.jpg)
Log Levels
![Page 42: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/42.jpg)
RFC: 5434 (The Syslog Protocol)
0 Emergency: system is unusable 1 Alert: action must be taken immediately 2 Critical: critical conditions 3 Error: error conditions 4 Warning: warning conditions 5 Notice: normal but significant condition 6 Informational: informational messages 7 Debug: debug-level messages
![Page 43: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/43.jpg)
02-LogLevels.php
use Monolog\Logger;use Monolog\Handler\StreamHandler;
$environment = getenv('ENVIRONMENT');$level = ($environment == "live") ? Logger::NOTICE : Logger::DEBUG;
// Create a New MonoLog$logger = new Monolog\Logger('Application Log');// Add a handler (writer)$logger->pushHandler(new StreamHandler('/tmp/user.log', $level));// Create a mapper$mapper = new UserMapper($logger);
// Create Users and persist them$logger->notice('Beginning User Creation');
while(true){ $user = new User(rand(1, 10000), 'Betty Boo');
$mapper->save($user);
sleep(1);
unset($user);}
![Page 44: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/44.jpg)
UserMapper.php
use Monolog\Logger;
class UserMapper {
protected $logger;
public function __construct(Logger $logger) { $this->logger = $logger; }
public function save(User $user) {
$this->logger->info('Saving User: ' . $user->getID());
// Code For Saving User
}
}
![Page 45: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/45.jpg)
DEMO
![Page 46: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/46.jpg)
3) Your Application Logger
https://github.com/benwaine/ApplicationLoggingTalk/tree/master/examples/03-YourApplicationLogClass
![Page 47: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/47.jpg)
Logger.php - __constructor
public function __construct(MLogger $application, MLogger $security, array $requestParams) {
$this->application = $application; $this->security = $security; $this->requestParams = $requestParams;
$this->requestId = md5(microtime() . $this->requestParams['REMOTE_ADDR']); }
![Page 48: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/48.jpg)
Logger.php - applicationLog
/** * Adds a record to the application log. * * @param string $message * @param int $level * * @return void */ public function applicationLog($message, $level) {
// Including a request identifier allows us to track a // users progress throughout the application by grepping the ID.
$context = array('RID' => $this->requestId);
$this->application->addRecord($level, $message, $context); }
![Page 49: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/49.jpg)
01-basic.php 1/2
// Create first logger (application)$applicationLogger = new MonoLogger('Application');// Add a handler (writer)$applicationLogger->pushHandler(new StreamHandler('/tmp/application.log', MonoLogger::DEBUG));
// Create second logger (security)$securityLogger = new MonoLogger('Security');$securityLogger->pushHandler(new StreamHandler('/tmp/security.log', MonoLogger::DEBUG));
$logger = new Logger($applicationLogger, $securityLogger, $requestParams);
// Create a mapper$mapper = new UserMapper($logger);
![Page 50: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/50.jpg)
01-basic.php 2/2
// Create Users and persist them$logger->applicationLog('Beginning User Creation', Logger::INFO);
for($x = 0; $x <= 4; $x++){ $user = new User(rand(1, 10000), 'Betty Boo');
// The mapper generates some informational logs when the // the user record is 'saved' $mapper->save($user); unset($user);
if($x % 2) {
// Simulate some kind of security warning when creating some // of the users. $logger->securityLog('UNSAFE USER!', Logger::WARNING); }}
$logger->applicationLog('Ending User Creation', Logger::INFO);
![Page 51: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/51.jpg)
DEMO
![Page 52: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/52.jpg)
BEN TURN ON THE OTHER EXAMPLE NOW.
Thanks.
![Page 53: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/53.jpg)
• Consolidated logging control logic
• Encapsulate common logging actions
• Still ‘Unwieldy’ instantiation
• Can be a little inflexible
Pros
Cons
![Page 54: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/54.jpg)
4) Your Application Logger(DI’ed)
https://github.com/benwaine/ApplicationLoggingTalk/tree/master/examples/04-DiConfiguredLogging
![Page 55: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/55.jpg)
parameters: logger.class: Logger logger.security.path: /tmp/security.log logger.security.name: security logger.security.level: 200 logger.application.path: /tmp/application.log logger.application.name: application logger.application.level: 200 logger.monolog.class: Monolog\Logger logger.monolog.handlerClass: Monolog\Handler\StreamHandler user.mapper.class: UserMapper request.params: REMOTE_ADDR: - 127.0.0.1
services.yml 1/2
![Page 56: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/56.jpg)
services: logger: class: %logger.class% arguments: [@applog, @seclog, %request.params%] seclog: class: %logger.monolog.class% arguments: [%logger.security.name%] calls: - [ pushHandler, [ @seclogHandler ] ] applog: class: %logger.monolog.class% arguments: [%logger.application.name%] calls: - [ pushHandler, [ @applogHandler ] ] seclogHandler: class: %logger.monolog.handlerClass% arguments: [%logger.security.path%, %logger.security.level%] applogHandler: class: %logger.monolog.handlerClass% arguments: [%logger.application.path%, %logger.application.level%] UserMapper: class: %user.mapper.class% arguments: [@logger]
services.yml 2/2
![Page 57: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/57.jpg)
01-basic.php 1/2
// Set Up Container$container = new ContainerBuilder;$loader = new YamlFileLoader($container, new FileLocator(__DIR__));$loader->load("services.yml");
// Get Mapper and Logger$logger = $container->get('logger');
![Page 58: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/58.jpg)
Me
![Page 59: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/59.jpg)
![Page 60: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/60.jpg)
5) Event Logging
https://github.com/benwaine/ApplicationLoggingTalk/tree/master/examples/05-StatsDGraphite
![Page 61: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/61.jpg)
Log Entries As Events That Occur In Your
System
![Page 62: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/62.jpg)
![Page 63: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/63.jpg)
![Page 64: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/64.jpg)
Graphite + StatsD
![Page 65: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/65.jpg)
StatsD
• Buckets
• Values
• Flush
![Page 66: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/66.jpg)
// Creating StatsD Client $connection = new \Domnikl\Statsd\Connection\Socket('localhost', 8125);$statsd = new \Domnikl\Statsd\Client($connection, "test.namespace");
// simple count$statsd->increment("request");
![Page 67: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/67.jpg)
Logger.php (extract)
public function logBusinessEvent($event) {
if(!isset($this->events[$event])) { throw new \Exception('Invalid Logging Event'); }
$this->statsd->increment($this->events[$event]);
}
public function logBusinessTime($event, $time) { if(!isset($this->events[$event])) { throw new \Exception('Invalid Logging Event'); }
$this->statsd->timing($this->events[$event], $time); }
![Page 68: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/68.jpg)
02-example.php (extract)
while(true) {
$number = rand(1, 10);
for($x = 0; $x <= $number; $x++) {
$user = new User(rand(1, 10000), 'Betty Boo'); $mapper->save($user); $logger->logBusinessEvent(Logger::EVENT_USERCREATED);
unset($user);
if($x%2 === 0) { // Simulate some kind of security warning when // creating some of the users. $logger->logBusinessEvent(Logger::EVENT_USERWARNING); } }
sleep(rand(1, 15));
}
![Page 69: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/69.jpg)
5) Putting It All Together
https://github.com/benwaine/ApplicationLoggingTalk/tree/master/examples/06-AllTogether
![Page 70: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/70.jpg)
Roadmap
• Introduction - Why Is Logging Good?
• The Mechanics - Code / Examples / Live Demos (GULP)
• Logging Strategy - What to do with your logs
• Conclusion
![Page 71: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/71.jpg)
Logging StrategyStyle
1) Choose a log format and be consistent
2) Make sure common tasks like formatting and filtering are dealt with centrally.
3) Choose a set of rules / style around when to log and be consistent.
![Page 72: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/72.jpg)
1) Request Start / End (with times and memory)
2) Transition between layersEg - Control > Model > DB < Model < Control > View
3) Database Queries
4) Business Events
5) Message Queue Activity
6) PHP Errors / PHP Notices
Logging StrategyContent
![Page 73: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/73.jpg)
1) Request Start / End (with times and memory)
2) Transition between layersEg - Control > Model > DB < Model < Control > View
3) Database Queries
4) Business Events
5) Message Queue Activity
6) PHP Errors / PHP Notices
Notice
InfoInfo / Debug
Separate Log
Debug
Error
Logging StrategyVolume
![Page 74: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/74.jpg)
1) Make it easy to switch log levels (environment variables).
2) Always think of your log consumers when adding log lines.
Logging StrategyAudience
![Page 75: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/75.jpg)
Ideas
Grep-able Content
Blameable
![Page 76: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/76.jpg)
Log Aggregation & Presentation
![Page 77: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/77.jpg)
RSYSLOG + LOGSTASH +
STATSD +GRAPHITE
![Page 78: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/78.jpg)
RSYSLOG •Send Logs Via TCP / UDP To A Central Location
•Use Filters & Templates To Write Logs To Files / Database
Web
DB
LOG
/var/log/web/%ip-address%/access.log
/var/log/db/%ip-address%/query.log
/var/log/web/%ip-address%/error.log
/var/log/db/%ip-address%/slow-query.log
![Page 79: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/79.jpg)
LOGSTASHinput { file { path => "/var/log/apache/access.log" type => "apache-access" }}
filter { grok { type => "apache-access" pattern => "%{COMBINEDAPACHELOG}" }}
output { statsd { # Count one hit every event by response increment => "apache.response.%{response}" }}
http://logstash.net/docs/1.1.1/tutorials/metrics-from-logs
![Page 80: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/80.jpg)
LOGSTASHOutputs
1 amqp2 boundary3 circonus4 datadog5 elasticsearch6 elasticsearch_http7 elasticsearch_river8 email9 file10 ganglia11 gelf12 graphite13 graphtastic14 http15 internal16 irc17 juggernaut18 librato19 loggly20 metriccatcher21 mongodb22 stomp
Inputs
1 amqp2 eventlog3 exec4 file5 ganglia6 gelf7 generator8 heroku9 irc10 log4j11 pipe12 redis13 stdin14 stomp15 syslog16 tcp17 twitter18 udp19 xmpp20 zeromq
Filters
1 checksum2 csv3 date4 dns5 environment6 gelfify7 grep8 grok9 grokdiscovery10 json11 multiline12 mutate13 noop14 split15 syslog_pri16 xml17 zeromq
23 nagios24 nagios_nsca25 null26 opentsdb27 pagerduty28 pipe29 redis30 riak31 riemann32 sns33 statsd34 stdout 35 tcp36 websocket37 xmpp38 zabbix39 zeromq
![Page 81: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/81.jpg)
DEMO
![Page 82: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/82.jpg)
Hosted Services
![Page 83: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/83.jpg)
![Page 84: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/84.jpg)
![Page 85: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/85.jpg)
![Page 86: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/86.jpg)
Roadmap
• Introduction - Why Is Logging Good?
• The Mechanics - Code / Examples / Live Demos (GULP)
• Logging Strategy - What to do with your logs
• Conclusion
![Page 87: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/87.jpg)
![Page 88: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/88.jpg)
![Page 89: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/89.jpg)
Fin - Questions?
Ask me to repeat the question. I ALWAYS forget.
![Page 90: The Art Of Application Logging PHPNW12](https://reader033.vdocument.in/reader033/viewer/2022051411/5478ef355906b585048b4635/html5/thumbnails/90.jpg)
Hiring Developers & Testers (Contract / Perm)