Issue 137: Save the last log to a file.
Status:  Done
Owner:
Closed:  Jan 2010
Project Member Reported by tommy.bjorling@gmail.com, Dec 9, 2009
Enhancement:
What about making the log saved after each application run?

After each application, there is a log. If we save the log in a folder, it 
would be easyer for end users to get some help from developers.

Jan 5, 2010
Project Member #1 dennd...@gmail.com
I tried to implement this once, but stopped because there's a circular import in logger.py, where I need to 
import pymt.pymt_config, but pymt imports logger.

Perhaps this will work as soon as rpavlik's work is merged?

Just so the patch doesn't get lost, here's what I did up to then:
http://paste.pocoo.org/show/156526/

diff --git a/pymt/__init__.py b/pymt/__init__.py
index 1070d5d..18cc5c4 100644
--- a/pymt/__init__.py
+++ b/pymt/__init__.py
@@ -13,10 +13,11 @@ from ConfigParser import ConfigParser
 import sys
 import getopt
 import os
+from os.path import join
 from logger import pymt_logger, LOG_LEVELS
 
 # Version number of current configuration format
-PYMT_CONFIG_VERSION = 4
+PYMT_CONFIG_VERSION = 5
 
 # Global settings options for pymt
 options = {
@@ -162,6 +163,12 @@ if not os.path.basename(sys.argv[0]).startswith('sphinx'):
             # add multisamples
             pymt_config.setdefault('graphics', 'multisamples', '2')
 
+        elif pymt_config_version == 4:
+            # add option to set logging path
+            log_dir = join(pymt_home_dir, 'logs')
+            mkdir(log_dir)
+            pymt_config.setdefault('pymt', 'log_file', join(log_dir, "%d-%m-%y.log"))
+
         else:
             # for future.
             pass
diff --git a/pymt/logger.py b/pymt/logger.py
index e58d6df..a9b1725 100644
--- a/pymt/logger.py
+++ b/pymt/logger.py
@@ -24,6 +24,8 @@ Examples of usage ::
 import logging
 import os
 import sys
+from time import strftime
+from pymt import pymt_config
 
 __all__ = ('pymt_logger', 'LOG_LEVELS', 'COLORS', 'pymt_logger_history')
 
@@ -78,13 +80,23 @@ class ColoredFormatter(logging.Formatter):
             record.levelname = levelname_color
         return logging.Formatter.format(self, record)
 
-class ColoredLogger(logging.Logger):
+class ConsoleAndFileLogger(logging.Logger):
+    """
+    Sets up console logging with colors and logging to a file. The file's path
+    can be specified in the config.
+    """
     FORMAT = '[%(levelname)-18s] %(message)s'
     COLOR_FORMAT = formatter_message(FORMAT, use_color)
 
     def __init__(self, name):
         global use_color
         logging.Logger.__init__(self, name, logging.DEBUG)
+        # Set up the file handler
+        filepath = strftime(pymt_config.getstr('pymt', 'log_file'))
+        filehandler = logging.FileHandler(filepath)
+        filehandler.setFormatter(logging.Formatter(self.FORMAT))
+        self.addHandler(filehandler)
+        # Set up the console handler with colors
         color_formatter = ColoredFormatter(self.COLOR_FORMAT, use_color=use_color)
         console = logging.StreamHandler()
         console.setFormatter(color_formatter)
@@ -95,9 +107,8 @@ class ColoredLogger(logging.Logger):
         else:
             self.addHandler(console)
         self.addHandler(HistoryHandler())
-        return
 
-logging.setLoggerClass(ColoredLogger)
+
+logging.setLoggerClass(ConsoleAndFileLogger)
 pymt_logger = logging.getLogger('PyMT')
 pymt_logger_history = HistoryHandler
-
Jan 14, 2010
Project Member #2 txprog
Done with the idea paster in the issue. Thanks dennda and tommy !
Status: Done
Owner: txprog
Labels: Milestone-0.4 Component-Core Type-Enhancement