Introduction to Logging. Application Logging



Similar documents
Logging in Java Applications

Overview. NetBorder Express Loggers Configuration Guide

The Java Logging API and Lumberjack

SAS 9.4 Logging. Configuration and Programming Reference Second Edition. SAS Documentation

SAS 9.3 Logging: Configuration and Programming Reference

Cello How-To Guide. Exception Management

How to Enable Quartz Job Execution Log Interception In Applications. J u n e 26,

SAS 9.2 Enhanced Logging Facilities. Session Wednesday, March 19, :00 9:50, Room 212

Determine the process of extracting monitoring information in Sun ONE Application Server

latest Release 0.2.6

Runtime Monitoring & Issue Tracking

Job Scheduler Daemon Configuration Guide

Logging. Working with the POCO logging framework.

NNMi120 Network Node Manager i Software 9.x Essentials

Oracle WebLogic Server

Universal Event Monitor for SOA Reference Guide

Elixir Schedule Designer User Manual

System Administration Guide

Configuring the Bundled SESM RADIUS Server

Configuring a Jetty Container for SESM Applications

IBM Software Services for Lotus Consulting Education Accelerated Value Program. Log Files IBM Corporation

Troubleshooting for Yamaha router

Event Center (rev b) EVENT CENTER. VPI 160 Camino Ruiz, Camarillo, CA (Voice) (Fax)

Setting Up the Site Licenses

FirstClass Synchronization Services 12 Release Notes Windows Version

Grid Engine 6. Troubleshooting. BioTeam Inc.

1. Stem. Configuration and Use of Stem

DevKey Documentation. Release 0.1. Colm O Connor

Handle Tool. User Manual

Configuring Log Files and Filtering Log Messages for Oracle WebLogic Server c (12.1.3)

A Sample OFBiz application implementing remote access via RMI and SOAP Table of contents

NativeMicrosoftOutlookConfiguration Guide. forversion2.3.1

Advanced Web Security, Lab

As shown, the emulator instance connected to adb on port 5555 is the same as the instance whose console listens on port 5554.

HOD Scheduler. Table of contents

Cloud Services. Introduction...2 Overview...2. Security considerations Installation...3 Server Configuration...4

Documentum Developer Program

FAQ: Understanding BlackBerry Enterprise Server Debug Logs

LANDESK Service Desk. Desktop Manager

MS Enterprise Library 5.0 (Logging Application Block)

Implementing Rexx Handlers in NetRexx/Java/Rexx

How to utilize Administration and Monitoring Console (AMC) in your TDI solution

FirstClass Export Tool User and Administration Guide

Stripe. Chapters. Copyright. Authors. Stripe modules for oscommerce Online Merchant. oscommerce Online Merchant v2.3

UltraLog HSPI User s Guide

Kaseya 2. User Guide. Version 7.0. English

I don t intend to cover Python installation, please visit the Python web site for details.

COMMANDS 1 Overview... 1 Default Commands... 2 Creating a Script from a Command Document Revision History... 10


Configuration Manager

Things you didn't know about Python

Healthstone Monitoring System

Router CLI Overview. CradlePoint, Inc.

IceWarp to IceWarp Server Migration

Syslog Monitoring Feature Pack

Oracle WebLogic Server

IBM WebSphere Partner Gateway V6.2.1 Advanced and Enterprise Editions

vmprof Documentation Release 0.1 Maciej Fijalkowski, Antonio Cuni, Sebastian Pawlus

ReplixFax Batch Loader

Configuring Logging. Configuring application logging with the LoggingConfigurator class.

Logging Service and Log Viewer for CPC Monitoring

$ftp = Net::FTP->new("some.host.name", Debug => 0) or die "Cannot connect to some.host.name: $@";

Audit Logging. Overall Goals

Sonatype CLM Enforcement Points - Continuous Integration (CI) Sonatype CLM Enforcement Points - Continuous Integration (CI)

Ease the rsyslog admin's life... Rainer Gerhards

CSE331: Introduction to Networks and Security. Lecture 12 Fall 2006

PaperCut Payment Gateway Module PayPal Website Payments Standard Quick Start Guide

Cisco Setting Up PIX Syslog

To install Multifront you need to have familiarity with Internet Information Services (IIS), Microsoft.NET Framework and SQL Server 2008.

PHP Debugging. Draft: March 19, Christopher Vickery

Entites in Drupal 8. Sascha Grossenbacher Christophe Galli

VoIP Recorder V2 Setup Guide

Indumathi B & Ingo Engels 18 November 2015

metaengine DataConnect For SharePoint 2007 Configuration Guide

HP Data Protector Integration with Autonomy IDOL Server

Unless otherwise noted, all references to STRM refer to STRM, STRM Log Manager, and STRM Network Anomaly Detection.

Cross-platform event logging in Object Pascal

How to extend Puppet using Ruby

Eventlog to Syslog v4.5 Release 4.5 Last revised September 29, 2013

Windows Scheduled Task and PowerShell Scheduled Job Management Pack Guide for Operations Manager 2012

Digger Solutions. Intranet Open Source. Administrator s Guide

Getting Started With Delegated Administration

WORKING WITH LOAD BALANCING AND QUEUEING FOR ADOBE INDESIGN CS5 SERVER

FioranoMQ 9. High Availability Guide

Server Manager Performance Monitor. Server Manager Diagnostics Page. . Information. . Audit Success. . Audit Failure

Comodo Web Application Firewall Software Version 2.0

Z Documentation. Release 2. Bart Thate

Syslog (Centralized Logging and Analysis) Jason Healy, Director of Networks and Systems

Siebel Application Deployment Manager Guide. Siebel Innovation Pack 2013 Version 8.1/8.2 September 2013

How To Understand How A Process Works In Unix (Shell) (Shell Shell) (Program) (Unix) (For A Non-Program) And (Shell).Orgode) (Powerpoint) (Permanent) (Processes

Mobile application development with QML & PySide

Collecting Windows Security Audit Log data with NXLog and Sysmon. Collecting Windows Security Audit Log data with NXLog and Sysmon

Configuring System Message Logging

The HTTP Plug-in. Table of contents

SendMIME Pro Installation & Users Guide

CommonSpot Content Server Version 6.2 Release Notes

Transcription:

Introduction to Logging David Beazley Copyright (C) 2008 http://www.dabeaz.com Note: This is a supplemental subject component to Dave's Python training classes. Details at: http://www.dabeaz.com/python.html Last Update : March 22, 2009 Application Logging Complex systems are often instrumented with some kind of logging capability Debugging and diagnostics Auditing Security Performance statistics Example : Web server logs 2

Error Handling Even in small programs, you often run into subtle issues related to exception handling for line in open("portfolio.dat"): fields = line.split() try: name = fields[0] shares = int(fields[1]) price = float(fields[2]) except ValueError:???? Do you print a warning? print "Bad line", line Do you ignore the error? pass Short answer: It depends 3 The Logging Problem Logging is a problem that seems simple, but usually isn't in practice. Problems: Figuring out what to log Where to send log data Log data management Implementation details 4

Homegrown Solutions Observation: Most significant applications will implement some kind of logging facility. Homegrown solutions tend to grow into a huge tangled hack. No one actually wants to work on logging--- it's often added as a half-baked afterthought. 5 logging Module A module for adding a logging capability to your application. A Python port of log4j (Java/Apache) Highly configurable Implements almost everything that you could possibly want for a logging framework 6

Logger Objects logging module relies on "Logger" objects A Logger is a target for logging messages Routes log data to one or more "handlers" Handler msg Logger Handler... Handler 7 Logging Big Picture Creating Logger objects Sending messages to a Logger object Attaching handlers to a Logger Configuring Logger objects 8

Getting a Logger To create/fetch a Logger object log = logging.getlogger("logname") If this is the first call, a new Logger object is created and associated with the given name If a Logger object with the given name was already created, a reference to that object is returned. This is used to avoid having to pass Logger objects around between program modules. 9 Logging Messages The logging module defines 5 logging "levels" CRITICAL 50 Critical errors ERROR 40 Error messages WARNING 30 Warning messages INFO 20 Information messages DEBUG 10 Debugging messages A logging message consists of a logging level and a logging message string (ERROR,"message") Logger Handler Handler 10

How to Issue a Message Methods for writing to the log log.critical(fmt [, *args ]) log.error(fmt [, *args ]) log.warning(fmt [, *args ]) log.info(fmt [, *args ]) log.debug(fmt [, *args ]) These are always used on some Logger object import logging log = logging.getlogger("logname") log.info("hello World") log.critical("a critical error occurred") 11 Logging Messages Logging functions work like printf log.error("filename '%s' is invalid", filename) log.error("errno=%d, %s", e.errno,e.strerror) log.warning("'%s' doesn't exist. Creating it", filename) Here is sample output Filename 'foo.txt' is invalid errno=9, Bad file descriptor 'out.dat' doesn't exit. Creating it 12

Logging Exceptions Messages can optionally include exception info try:... except RuntimeError: log.error("update failed",exc_info=true) Will include traceback info from the current exception (if any) Sample output with exception traceback Update failed Traceback (most recent call last): File "<stdin>", line 2, in <module> RuntimeError: Invalid user name 13 Example Usage Logging often gets added as an optional feature def read_portfolio(filename,log=none): for line in open(filename): fields = line.split() try: name = fields[0] shares = int(fields[1]) price = float(fields[2]) except ValueError: if log: log.warning("bad line: %s",line) By doing this, the handling of the log message becomes user-configurable More flexible than just hard-coding a print 14

Log Handlers A Logger object only receives messages It does not produce any output Must use a handler to output messages Msg Logger Handler Handler Output 15 Attaching a Handler Example of attaching a handler to a Logger import logging, sys # Create a logger object log = logging.getlogger("logname") # Create a handler object stderr_hand = logging.streamhandler(sys.stderr) # Attach handler to logger log.addhandler(stderr_hand) # Issue a log message (routed to sys.stderr) log.error("an error message") 16

Attaching Multiple Handlers Sending messages to stderr and a file import logging, sys # Create a logger object log = logging.getlogger("logname") # Create handler objects stderr_hand = logging.streamhandler(sys.stderr) logfile_hand = logging.filehandler("log.txt") # Attach the handlers to logger log.addhandler(stderr_hand) log.addhandler(logfile_hand) # Issue a log message. Message goes to both handlers log.error("an error message") 17 Handler Types There are many types of handlers logging.streamhandler logging.filehandler logging.handlers.rotatingfilehandler logging.handlers.timedrotatingfilehandler logging.handlers.sockethandler logging.handlers.datagramhandler logging.handlers.smtphandler logging.handlers.sysloghandler logging.handlers.nteventloghandler logging.handlers.memoryhandler logging.handlers.httphandler Consult a reference for details. More examples later 18

Level Filtering All messages have numerical "level" 50 CRITICAL 40 ERROR 30 WARNING 20 INFO 10 DEBUG 0 NOTSET Each Logger has a level filter log.setlevel(logging.info) Only messages with a level higher than the set level will be forwarded to the handlers 19 Level Filtering All Handlers also have a level setting stderr_hand = logging.streamhander(sys.stderr) stderr_hand.setlevel(logging.info) Handlers only produce output for messages with a level higher than their set level Each Handler can have its own level setting 20

Level Filtering Logger Handler messages level level Output Big picture : Different objects are receiving the messages and only responding to those messages that meet a certain level threshold Logger level is a "global" setting Handler level is just for that handler. 21 Level Filtering Example # Create handler objects stderr_hand = logging.streamhandler(sys.stderr) stderr_hand.setlevel(logging.critical) logfile_hand = logging.filehandler("log.txt") logfile_hand.setlevel(logging.debug) Logger stderr_hand (CRITICAL) messages level level logfile_hand (DEBUG) level 22

Advanced Filtering All log messages can be routed through a filter object. # Define a filter object. Must implement.filter() class MyFilter(logging.Filter): def filter(self,logrecord): # Return True/False to keep message... # Create a filter object myfilt = MyFilter() # Attach it to a Logger object log.addfilter(myfilt) # Attach it to a Handler object hand.addfilter(myfilt) 23 Advanced Filtering Filter objects receive a LogRecord object class MyFilter(logging.Filter): def filter(self,logrecord):... LogRecord attributes logrecord.name logrecord.levelno logrecord.levelname logrecord.pathname logrecord.filename logrecord.module logrecord.lineno logrecord.created logrecord.asctime logrecord.thread logrecord.threadname logrecord.process logrecord.message Name of the logger Numeric logging level Name of logging level Path of source file Filename of source file Module name Line number Time when logging call executed ASCII-formated date/time Thread-ID Thread name Process ID Logged message 24

Filtering Example Only produce messages from a specific module class ModuleFilter(logging.Filter): def init (self,modname): logging.filter. init (self) self.modname = modname def filter(self,logrecord): return logrecord.module == self.modname log = getlogger("logname") modfilt = ModuleFilter("somemod") log.addfilter(modfilt) 25 Multiple Filters Multiple Filters may be added log.addfilter(f) log.addfilter(g) log.addfilter(h) Messages must pass all to be output Filters can be removed later log.removefilter(f) 26

Log Message Format By default, log messages are just the message log.error("an error occurred") An error occurred However, you can add more information Logger name and level Thread names Date/time 27 Customized Formatters Create a Formatter object # Create a message format msgform = logging.formatter( "%(levelname)s:%(name)s:%(asctime)s:%(message)s" ) # Create a handler stderr_hand = logging.streamhandler(sys.stderr) # Set the handler's message format stderr_hand.setformatter(msgform) Formatter determines what gets put in output ERROR:logname:2007-01-24 11:27:26,286:Message 28

Message Format Special format codes %(name)s %(levelno)s %(levelname)s %(pathname)s %(filename)s %(module)s %(lineno)d %(created)f %(asctime)s %(thread)d %(threadname)s %(process)d %(message)s Name of the logger Numeric logging level Name of logging level Path of source file Filename of source file Module name Line number Time when logging call executed ASCII-formated date/time Thread-ID Thread name Process ID Logged message Information is specific to where logging call was made (e.g., source file, line number, etc) 29 Message Formatting Each Handler has a single message formatter Use setformatter() to set it hand.setformatter(form) Different handlers can have different message formats 30

Multiple loggers An application may have many loggers Each is identified by a logger name netlog = logging.getlogger("network") guilog = logging.getlogger("gui") thrlog = logging.getlogger("threads") Each logger object is independent Has own handlers, filters, levels, etc. 31 Hierarchical Loggers Loggers can be organized in a name hierarchy applog = logging.getlogger("app") netlog = logging.getlogger("app.network") conlog = logging.getlogger("app.network.connection") guilog = logging.getlogger("app.gui") thrlog = logging.getlogger("app.threads") Messages flow up the name hierarchy app app.network app.gui app.threads app.network.connection 32

Hierarchical Loggers With a hierarchy, filters and handlers can be attached to every single Logger involved The level of a child logger is inherited from the parent unless set directly To prevent message forwarding on a Logger: log.propagate = False Commentary: Clearly it can get quite advanced 33 The Root Logger Logging module optionally defines a "root" logger to which all logging messages are sent Initialized if you use one of the following: logging.critical() logging.error() logging.warning() logging.info() logging.debug() 34

The Root Logger Root logger is useful for quick solutions and short scripts import logging logging.basicconfig( level = logging.info, filename = "log.txt", format = "%(levelname)s:%(asctime)s:%(message)s" ) logging.info("my program is starting")... 35 Putting it Together Adding logging to your program involves two steps Adding support for logging objects and adding statements to issue log messages Providing a means for configuring the logging environment Let's briefly talk about the second point 36

Logging Configuration Logging is something that frequently gets reconfigured (e.g., during debugging) To configure logging for your application, there are two approaches you can take Isolate it to a well-known module Use config files (ConfigParser) 37 A Sample Configuration A sample configuration module # logconfig.py import logging, sys # Set the message format format = logging.formatter( "%(levelname)-10s %(asctime)s %(message)s") # Create a CRITICAL message handler crit_hand = logging.streamhandler(sys.stderr) crit_hand.setlevel(logging.critical) crit_hand.setformatter(format) # Create a handler for routing to a file applog_hand = logging.filehandler('app.log') applog_hand.setformatter(format) # Create a top-level logger called 'app' app_log = logging.getlogger("app") app_log.setlevel(logging.info) app_log.addhandler(applog_hand) app_log.addhandler(crit_hand) 38

A Sample Configuration To use the previous configuration, you import # main.py import logconfig import otherlib... Mainly, you just need to make sure the logging gets set up before other modules start using it In other modules... import logging log = logging.getlogger('app')... log.critical("an error occurred") 39 Using a Config File You can also configure with an INI file ; logconfig.ini [loggers] keys=root,app [handlers] keys=crit,applog [formatters] keys=format [logger_root] level=notset handlers= [logger_app] level=info propagate=0 qualname=app handlers=crit,applog [handler_crit] class=streamhandler level=critical formatter=format args=(sys.stderr,) [handler_applog] class=filehandler level=notset formatter=format args=('app.log',) [formatter_format] format=%(levelname)-10s %(asctime)s %(message)s datefmt= 40

Reading a Config File To use the previous configuration use this # main.py import logging.config logging.config.fileconfig('logconfig.ini')... The main advantage of using an INI file is that you don't have to go in and modify your code Easier to have a set of different configuration files for different degrees of logging For example, you could have a production configuration and a debugging configuration 41 Summary There are many more subtle configuration details concerning the logging module However, this is enough to give you a small taste of using it in order to get started 42