1 | # This file is part of CMT, a Cluster Management Tool made at SARA. |
---|
2 | # Copyright (C) 2012 Sil Westerveld |
---|
3 | # |
---|
4 | # This program is free software; you can redistribute it and/or modify |
---|
5 | # it under the terms of the GNU General Public License as published by |
---|
6 | # the Free Software Foundation; either version 2 of the License, or |
---|
7 | # (at your option) any later version. |
---|
8 | # |
---|
9 | # This program is distributed in the hope that it will be useful, |
---|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | # GNU General Public License for more details. |
---|
13 | # |
---|
14 | # You should have received a copy of the GNU General Public License |
---|
15 | # along with this program; if not, write to the Free Software |
---|
16 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
17 | |
---|
18 | import logging |
---|
19 | import logging.config |
---|
20 | |
---|
21 | |
---|
22 | class Logger: |
---|
23 | """ |
---|
24 | A logging facility, implemented with the borg design pattern. Makes |
---|
25 | use of the Python logging lib, see: |
---|
26 | http://docs.python.org/library/logging.html |
---|
27 | """ |
---|
28 | __shared_state = {} |
---|
29 | |
---|
30 | # Check for existence of a global logging object, otherwise make one |
---|
31 | if 'logger' not in __shared_state.keys(): |
---|
32 | #TODO: think about a (better) way to make this dynamic: |
---|
33 | import site |
---|
34 | |
---|
35 | if site.sys.prefix in [ '/usr', '/' ]: |
---|
36 | ETC_PREPEND = '' |
---|
37 | else: |
---|
38 | ETC_PREPEND = site.sys.prefix |
---|
39 | |
---|
40 | logging.config.fileConfig('%s/etc/cmt/logging.conf'%ETC_PREPEND) |
---|
41 | __shared_state['logger'] = logging.getLogger('cli') |
---|
42 | |
---|
43 | def __init__(self): |
---|
44 | """ |
---|
45 | Assigns the global __shared_state to __dict__ to be able to use |
---|
46 | the class like a singleton. |
---|
47 | """ |
---|
48 | self.__dict__ = self.__shared_state |
---|
49 | |
---|
50 | def getLogger(self): |
---|
51 | """ |
---|
52 | Returns the only logger instance (or state, to be exactly). |
---|
53 | """ |
---|
54 | return self.__dict__['logger'] |
---|