Show More
@@ -19,11 +19,12 b' Authors:' | |||||
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 |
|
20 | |||
21 | from copy import deepcopy |
|
21 | from copy import deepcopy | |
|
22 | import logging | |||
22 | import sys |
|
23 | import sys | |
23 |
|
24 | |||
24 | from IPython.config.configurable import Configurable |
|
25 | from IPython.config.configurable import SingletonConfigurable | |
25 | from IPython.utils.traitlets import ( |
|
26 | from IPython.utils.traitlets import ( | |
26 | Unicode, List |
|
27 | Unicode, List, Int | |
27 | ) |
|
28 | ) | |
28 | from IPython.config.loader import ( |
|
29 | from IPython.config.loader import ( | |
29 | KeyValueConfigLoader, PyFileConfigLoader |
|
30 | KeyValueConfigLoader, PyFileConfigLoader | |
@@ -34,7 +35,7 b' from IPython.config.loader import (' | |||||
34 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
35 |
|
36 | |||
36 |
|
37 | |||
37 | class Application(Configurable): |
|
38 | class Application(SingletonConfigurable): | |
38 |
|
39 | |||
39 | # The name of the application, will usually match the name of the command |
|
40 | # The name of the application, will usually match the name of the command | |
40 | # line application |
|
41 | # line application | |
@@ -51,11 +52,32 b' class Application(Configurable):' | |||||
51 | # The version string of this application. |
|
52 | # The version string of this application. | |
52 | version = Unicode(u'0.0') |
|
53 | version = Unicode(u'0.0') | |
53 |
|
54 | |||
|
55 | log_level = Int(logging.WARN, config=True, shortname="log_level") | |||
|
56 | ||||
54 | def __init__(self, **kwargs): |
|
57 | def __init__(self, **kwargs): | |
55 | Configurable.__init__(self, **kwargs) |
|
58 | SingletonConfigurable.__init__(self, **kwargs) | |
56 | # Add my class to self.classes so my attributes appear in command line |
|
59 | # Add my class to self.classes so my attributes appear in command line | |
57 | # options. |
|
60 | # options. | |
58 | self.classes.insert(0, self.__class__) |
|
61 | self.classes.insert(0, self.__class__) | |
|
62 | self.init_logging() | |||
|
63 | ||||
|
64 | def init_logging(self): | |||
|
65 | """Start logging for this application. | |||
|
66 | ||||
|
67 | The default is to log to stdout using a StreaHandler. The log level | |||
|
68 | starts at loggin.WARN, but this can be adjusted by setting the | |||
|
69 | ``log_level`` attribute. | |||
|
70 | """ | |||
|
71 | self.log = logging.getLogger(self.__class__.__name__) | |||
|
72 | self.log.setLevel(self.log_level) | |||
|
73 | self._log_handler = logging.StreamHandler() | |||
|
74 | self._log_formatter = logging.Formatter("[%(name)s] %(message)s") | |||
|
75 | self._log_handler.setFormatter(self._log_formatter) | |||
|
76 | self.log.addHandler(self._log_handler) | |||
|
77 | ||||
|
78 | def _log_level_changed(self, name, old, new): | |||
|
79 | """Adjust the log level when log_level is set.""" | |||
|
80 | self.log.setLevel(new) | |||
59 |
|
81 | |||
60 | def print_help(self): |
|
82 | def print_help(self): | |
61 | """Print the help for each Configurable class in self.classes.""" |
|
83 | """Print the help for each Configurable class in self.classes.""" |
General Comments 0
You need to be logged in to leave comments.
Login now