how to listen for a os program log python code example

Example 1: python logging basiclogging rotatingfilehandler

import logging

rfh = logging.handlers.RotatingFileHandler(
 	filename='foo.log', 
  	mode='a',
  	maxBytes=20*1024*1024,
  	backupCount=2,
  	encoding=None,
  	delay=0
)

logging.basicConfig(
  	level=logging.DEBUG,
  	format="%(asctime)s %(name)-25s %(levelname)-8s %(message)s",
    datefmt="%y-%m-%d %H:%M:%S",
    handlers=[
    	rfh
    ]
)

logger = logging.getLogger('main')

logger.debug("test")

Example 2: from logging import logger

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import logging

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)

logger.debug('Loging %s lewel', 'DEBUG')
logger.info('Loging %s lewel', 'INFO')
logger.warning('Loging %s lewel', 'WARN')
logger.error('Loging %s lewel', 'ERROR')
logger.critical('Loging %s lewel', 'CRITICAL')

Tags:

Misc Example