From 9aad90c3237bfc779477a728655f26b0e4fb632e Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Fri, 24 Apr 2015 19:59:53 -0400 Subject: [PATCH] Added snippets of python logging with RotatingFileHandler and TimedRotatingFileHandler. --- python-data/logs.ipynb | 194 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 python-data/logs.ipynb diff --git a/python-data/logs.ipynb b/python-data/logs.ipynb new file mode 100644 index 0000000..a2184a0 --- /dev/null +++ b/python-data/logs.ipynb @@ -0,0 +1,194 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Logging in Python\n", + "* Logging with RotatingFileHandler\n", + "* Logging with TimedRotatingFileHandler " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Logging with RotatingFileHandler\n", + "\n", + "The logging discussion is taken from the [Python Logging Cookbook](https://docs.python.org/2/howto/logging-cookbook.html#using-file-rotation):\n", + "\n", + "Sometimes you want to let a log file grow to a certain size, then open a new file and log to that. You may want to keep a certain number of these files, and when that many files have been created, rotate the files so that the number of files and the size of the files both remain bounded. For this usage pattern, the logging package provides a RotatingFileHandler.\n", + "\n", + "The most current file is always logging_rotatingfile_example.out, and each time it reaches the size limit it is renamed with the suffix .1. Each of the existing backup files is renamed to increment the suffix (.1 becomes .2, etc.) and the .6 file is erased.\n", + "\n", + "The following code snippet is taken from [here](http://www.blog.pythonlibrary.org/2014/02/11/python-how-to-create-rotating-logs/)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import logging\n", + "import time\n", + " \n", + "from logging.handlers import RotatingFileHandler\n", + " \n", + "#----------------------------------------------------------------------\n", + "def create_rotating_log(path):\n", + " \"\"\"\n", + " Creates a rotating log\n", + " \"\"\"\n", + " logger = logging.getLogger(\"Rotating Log\")\n", + " logger.setLevel(logging.INFO)\n", + " \n", + " # add a rotating handler\n", + " handler = RotatingFileHandler(path, maxBytes=20,\n", + " backupCount=5)\n", + " logger.addHandler(handler)\n", + " \n", + " for i in range(10):\n", + " logger.info(\"This is test log line %s\" % i)\n", + " time.sleep(1.5)\n", + " \n", + "#----------------------------------------------------------------------\n", + "if __name__ == \"__main__\":\n", + " log_file = \"test.log\"\n", + " create_rotating_log(log_file)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Logging with TimedRotatingFileHandler\n", + "\n", + "The following code snippet is taken from [here](http://www.blog.pythonlibrary.org/2014/02/11/python-how-to-create-rotating-logs/)." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n", + "INFO:Rotating Log:This is a test!\n" + ] + } + ], + "source": [ + "import logging\n", + "import time\n", + " \n", + "from logging.handlers import TimedRotatingFileHandler\n", + " \n", + "#----------------------------------------------------------------------\n", + "def create_timed_rotating_log(path):\n", + " \"\"\"\"\"\"\n", + " logger = logging.getLogger(\"Rotating Log\")\n", + " logger.setLevel(logging.INFO)\n", + " \n", + " # Rotate log based on when parameter:\n", + " # second (s)\n", + " # minute (m)\n", + " # hour (h)\n", + " # day (d)\n", + " # w0-w6 (weekday, 0=Monday)\n", + " # midnight\n", + " handler = TimedRotatingFileHandler(path,\n", + " when=\"m\",\n", + " interval=1,\n", + " backupCount=5)\n", + " logger.addHandler(handler)\n", + " \n", + " for i in range(20):\n", + " logger.info(\"This is a test!\")\n", + " time.sleep(1.5)\n", + " \n", + "#----------------------------------------------------------------------\n", + "if __name__ == \"__main__\":\n", + " log_file = \"timed_test.log\"\n", + " create_timed_rotating_log(log_file)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "u'/Users/dmartin/Desktop/Dev/Vectorworks/Engineering/Nebula/Mainline/Servers/analytics/ipython-data-notebooks/python-data'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pwd" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.9" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}