gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / dulwich / log_utils.py @ 959
History | View | Annotate | Download (2.51 KB)
1 | 959 | jjdelcerro | # log_utils.py -- Logging utilities for Dulwich
|
---|---|---|---|
2 | # Copyright (C) 2010 Google, Inc.
|
||
3 | #
|
||
4 | # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
|
||
5 | # General Public License as public by the Free Software Foundation; version 2.0
|
||
6 | # or (at your option) any later version. You can redistribute it and/or
|
||
7 | # modify it under the terms of either of these two licenses.
|
||
8 | #
|
||
9 | # Unless required by applicable law or agreed to in writing, software
|
||
10 | # distributed under the License is distributed on an "AS IS" BASIS,
|
||
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
12 | # See the License for the specific language governing permissions and
|
||
13 | # limitations under the License.
|
||
14 | #
|
||
15 | # You should have received a copy of the licenses; if not, see
|
||
16 | # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
|
||
17 | # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
|
||
18 | # License, Version 2.0.
|
||
19 | #
|
||
20 | |||
21 | """Logging utilities for Dulwich.
|
||
22 |
|
||
23 | Any module that uses logging needs to do compile-time initialization to set up
|
||
24 | the logging environment. Since Dulwich is also used as a library, clients may
|
||
25 | not want to see any logging output. In that case, we need to use a special
|
||
26 | handler to suppress spurious warnings like "No handlers could be found for
|
||
27 | logger dulwich.foo".
|
||
28 |
|
||
29 | For details on the _NullHandler approach, see:
|
||
30 | http://docs.python.org/library/logging.html#configuring-logging-for-a-library
|
||
31 |
|
||
32 | For many modules, the only function from the logging module they need is
|
||
33 | getLogger; this module exports that function for convenience. If a calling
|
||
34 | module needs something else, it can import the standard logging module directly.
|
||
35 | """
|
||
36 | |||
37 | import logging |
||
38 | import sys |
||
39 | |||
40 | getLogger = logging.getLogger |
||
41 | |||
42 | |||
43 | class _NullHandler(logging.Handler): |
||
44 | """No-op logging handler to avoid unexpected logging warnings."""
|
||
45 | |||
46 | def emit(self, record): |
||
47 | pass
|
||
48 | |||
49 | |||
50 | _NULL_HANDLER = _NullHandler() |
||
51 | _DULWICH_LOGGER = getLogger('dulwich')
|
||
52 | _DULWICH_LOGGER.addHandler(_NULL_HANDLER) |
||
53 | |||
54 | |||
55 | def default_logging_config(): |
||
56 | """Set up the default Dulwich loggers."""
|
||
57 | remove_null_handler() |
||
58 | logging.basicConfig(level=logging.INFO, stream=sys.stderr, |
||
59 | format='%(asctime)s %(levelname)s: %(message)s')
|
||
60 | |||
61 | |||
62 | def remove_null_handler(): |
||
63 | """Remove the null handler from the Dulwich loggers.
|
||
64 |
|
||
65 | If a caller wants to set up logging using something other than
|
||
66 | default_logging_config, calling this function first is a minor optimization
|
||
67 | to avoid the overhead of using the _NullHandler.
|
||
68 | """
|
||
69 | _DULWICH_LOGGER.removeHandler(_NULL_HANDLER) |