use of monolog with php

26
Monolog in PHP Presenter : SATHEESKUMAR A Company : MINDFIRE SOLUTIONS Date : 04/06/2014

Upload: mindfire-solutions

Post on 10-May-2015

1.074 views

Category:

Software


2 download

DESCRIPTION

This presntation is all about logging in of data and errors using monolog. Anyone who works with php would find this useful for sure.

TRANSCRIPT

Page 1: Use of Monolog with PHP

Monolog in PHP

Presenter : SATHEESKUMAR ACompany : MINDFIRE SOLUTIONSDate : 04/06/2014

Page 2: Use of Monolog with PHP

Presenter: Satheeskumar A, Mindfire Solutions

About me➢ Zend Certified Engineer (ZCE)➢ Oracle Certified Mysql Professional (OCP – MYSQL)➢ 10 gen certified Mongodb professional ➢ Microsoft Certified HTML5 professional➢ Accrediated certified Scrum Master

Skills: PHP, Mysql, Symfony2, CodeIgniter, SVN, GIT, Doctrine, Propel ...

Connect me:Facebook: https://www.facebook.com/satheeskumar.ashokkumar/Twitter: https://twitter.com/sathees1kumarLinked in: in.linkedin.com/in/satheeskumara/Google Plus: https://plus.google.com/+satheeskumara

Contact me:

E-mail: [email protected] / [email protected]: mfsi_satheesk

Page 3: Use of Monolog with PHP

Presenter: Satheeskumar A, Mindfire Solutions

Agenda

What is Logging History of logging Need of Logging Current Conventions Use of Monolog Components of Monolog Different Types of Handlers Types of Formatters Tips and Tricks Sample Code

Page 4: Use of Monolog with PHP

Take Away

“To know the best method of logging data of different contexts for specific environments such as test/dev

and production”

Presenter: Satheeskumar A, Mindfire Solutions

Page 5: Use of Monolog with PHP

Logging

Presenter: Satheeskumar A, Mindfire Solutions

The process of using a computer to collect data through sensors/listeners/events.

Analyze the data and save and output the results of the collection and analysis.

Data logging also implies the control of how the computer collects and analyzes the data.

Page 6: Use of Monolog with PHP

History of Logging

Presenter: Satheeskumar A, Mindfire Solutions

Even with use of computers there was a real need to measure the overall performance of any reasearch

Early 1980's there was a Instrument called VELA (virtual laboratory) used for data harvesting

Late 1980's, A device was invented to collect information through sensors

Later then data logging/harvesting has been used widely in all applications/reasearches/products.

Page 7: Use of Monolog with PHP

Need of Logging

Presenter: Satheeskumar A, Mindfire Solutions

Track Users activity/Movement Transaction Logging Track user errors System level failures/warnings Research Data collection and Interpretation

Page 8: Use of Monolog with PHP

Types of Logging

Presenter: Satheeskumar A, Mindfire Solutions

Error / Exception logs Access logs System logs Application logs Database logs Transaction logs Mailer logsetc...

Page 9: Use of Monolog with PHP

Current Conventions - Apache/PHP

Presenter: Satheeskumar A, Mindfire Solutions

<VirtualHost *:80><Directory /var/www/html/>

Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined</VirtualHost>

Page 10: Use of Monolog with PHP

Monolog Enters Here...

Presenter: Satheeskumar A, Mindfire Solutions

Monolog is a PHP library that support different levels of logging for PHP Applications and depends on PSR.

Inspired by Python Logbook library

Provides stack of handlers

More Powerful than conventional way of logging in applications

Page 11: Use of Monolog with PHP

What's different ?

Presenter: Satheeskumar A, Mindfire Solutions

● Monolog sends your logs to files, sockets, inboxes, databases and various web services.

● Channel based approach

● Different stack of handlers for specific channels

● Pile up handler stack based on severity.

● Format Interpretation depending on severity and channel

● Prevents Bubbling when severity is reached

Page 12: Use of Monolog with PHP

Severity Levels

Presenter: Satheeskumar A, Mindfire Solutions

● DEBUG – 100● INFO – 200● NOTICE – 250● WARNING – 300● ERROR – 400● CRITICAL – 500● ALERT – 500● EMERGENCY – 600

Page 13: Use of Monolog with PHP

Components that make up

Presenter: Satheeskumar A, Mindfire Solutions

● Handlers● Formatters● Processors● Dependent packages like send_mail / FirePHP/

Chrome PHP etc..

Page 14: Use of Monolog with PHP

Handlers

Presenter: Satheeskumar A, Mindfire Solutions

● Each logger instance will have a stack of handlers.

● If no handlers are pushed to the stack, stream handler will handled as the default.

● The last pushed handler will be treated first.

● Other handlers are bubbled up based on severity

● Bubbling can be stopped at any level. Handler instances can be shared between multiple channels.

● Support to write to Database and cloud services

Page 15: Use of Monolog with PHP

Formatters

Presenter: Satheeskumar A, Mindfire Solutions

● Each handler has a default format.

● Custom Formatter instance can be assigned to any Handler.

● Formatters can be shared between different handlers

● Formatters can be different for different channels

● Line Formatter is the default formatter.

Page 16: Use of Monolog with PHP

Processors

Presenter: Satheeskumar A, Mindfire Solutions

● Processors to include more details before the handler renders the log message.

● Can be used to nail down the issue

● Used to measure the memory usage

● Introspection of running processess.

Page 17: Use of Monolog with PHP

Advantages

Presenter: Satheeskumar A, Mindfire Solutions

● Option to have different channel for different module

● Custom detailing

● Different handlers for different development

● Thorough participation in different stages of lifecycle

● Open for third party integration

● Readable and Beautiful Layered message

Page 18: Use of Monolog with PHP

Monolog in Development Env

Presenter: Satheeskumar A, Mindfire Solutions

● Use of FirePHPHandler and ChromePHPHandler

● FirePHPFormatting can be used to format the log messages

● Prevention of bubbling from writing to database

Page 19: Use of Monolog with PHP

Monolog in Production Env

Presenter: Satheeskumar A, Mindfire Solutions

● Rotatelog handler rotates log

● Send Beautiful error messages via HTML mails

● Write the log messages to any Database mysql, mongo, couch.

● Support with ORM packages like Doctrine etc..

● Can be integrated to cloud services like Loggly

● Keeps your log messages distinct for different modular functionality

Page 20: Use of Monolog with PHP

Lets dirty our hands with code

Presenter: Satheeskumar A, Mindfire Solutions

$log = new Logger('app');$log->pushHandler( new StreamHandler(__DIR__

. '/logs/error.log', Logger::NOTICE, false));$log->addNotice('this is a notice');$log->addNotice('this is an error');

Page 21: Use of Monolog with PHP

Sample Code in GIT

Presenter: Satheeskumar A, Mindfire Solutions

https://github.com/sathees1kumar/monolog

Feel free to fork and play :)

Page 22: Use of Monolog with PHP

Do you use Frameworks / CMS ?

Presenter: Satheeskumar A, Mindfire Solutions

● CakePHP - https://github.com/jadb/cakephp-monolog ● Symfony2 - https://github.com/symfony/MonologBundle● Slim – https://github.com/flynsarmy/Slim-Monolog● Zend2 - https://packagist.org/packages/enlitepro/enlite-monolog● CodeIgniter - https://github.com/pfote/Codeigniter-Monolog● Laravel – Inbuilt Support.● Drupal - https://drupal.org/project/monolog● Wordpress -

https://packagist.org/packages/fancyguy/wordpress-monolog

Page 23: Use of Monolog with PHP

References

Presenter: Satheeskumar A, Mindfire Solutions

The only link which is more than enough to get started with,

https://github.com/Seldaek/monolog

Page 24: Use of Monolog with PHP

?

Presenter: Satheeskumar A, Mindfire Solutions

Page 25: Use of Monolog with PHP

Thank you :)

Presenter: Satheeskumar A, Mindfire Solutions

Page 26: Use of Monolog with PHP

www.mindfiresolutions.com

https://www.facebook.com/MindfireSolutions

http://www.linkedin.com/company/mindfire-solutions

http://twitter.com/mindfires