mirror of
https://github.com/donnemartin/data-science-ipython-notebooks.git
synced 2024-03-22 13:30:56 +08:00
144 lines
4.6 KiB
Python
144 lines
4.6 KiB
Python
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/data-science-ipython-notebooks)."
|
|
]
|
|
},
|
|
{
|
|
"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": null,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"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)"
|
|
]
|
|
}
|
|
],
|
|
"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.10"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
}
|