diff --git a/hyde/ext/plugins/less.py b/hyde/ext/plugins/less.py index ba874ef..5d15f6a 100644 --- a/hyde/ext/plugins/less.py +++ b/hyde/ext/plugins/less.py @@ -9,10 +9,6 @@ import re import subprocess import traceback -import logging -from logging import NullHandler -logger = logging.getLogger('hyde.engine') -logger.addHandler(NullHandler()) class LessCSSPlugin(Plugin): """ @@ -78,8 +74,8 @@ class LessCSSPlugin(Plugin): try: subprocess.check_output([str(less), str(source), str(target)]) except subprocess.CalledProcessError, error: - logger.error(traceback.format_exc()) - logger.error(error.output) + self.logger.error(traceback.format_exc()) + self.logger.error(error.output) raise self.template.exception_class( "Cannot process less css. Error occurred when " "processing [%s]" % resource.source_file) diff --git a/hyde/ext/plugins/meta.py b/hyde/ext/plugins/meta.py index 28b7932..65c9233 100644 --- a/hyde/ext/plugins/meta.py +++ b/hyde/ext/plugins/meta.py @@ -6,11 +6,6 @@ from hyde.model import Expando from hyde.plugin import Plugin import yaml -import logging -from logging import NullHandler -logger = logging.getLogger('hyde.engine') -logger.addHandler(NullHandler()) - class Metadata(Expando): """ @@ -77,13 +72,13 @@ class MetaPlugin(Plugin): the resource. Load meta data by looking for the marker. Once loaded, remove the meta area from the text. """ - logger.info("Trying to load metadata from resource [%s]" % resource) + self.logger.info("Trying to load metadata from resource [%s]" % resource) yaml_finder = re.compile( r"^\s*(?:---|===)\s*\n((?:.|\n)+?)\n\s*(?:---|===)\s*\n", re.MULTILINE) match = re.match(yaml_finder, text) if not match: - logger.info("No metadata found in resource [%s]" % resource) + self.logger.info("No metadata found in resource [%s]" % resource) data = {} else: text = text[match.end():] @@ -96,7 +91,7 @@ class MetaPlugin(Plugin): else: resource.meta.update(data) self.__update_standard_attributes__(resource) - logger.info("Successfully loaded metadata from resource [%s]" + self.logger.info("Successfully loaded metadata from resource [%s]" % resource) return text diff --git a/hyde/ext/plugins/sorter.py b/hyde/ext/plugins/sorter.py index ffc07fc..23957e0 100644 --- a/hyde/ext/plugins/sorter.py +++ b/hyde/ext/plugins/sorter.py @@ -10,11 +10,6 @@ from functools import partial from itertools import ifilter, izip, tee from operator import attrgetter -import logging -from logging import NullHandler -logger = logging.getLogger('hyde.engine') -logger.addHandler(NullHandler()) - def pairwalk(iterable): a, b = tee(iterable) next(b, None) @@ -110,7 +105,7 @@ class SorterPlugin(Plugin): return for name, settings in config.sorter.__dict__.items(): - logger.info("Adding sort methods for [%s]" % name) + self.logger.info("Adding sort methods for [%s]" % name) sort_method_name = 'walk_resources_sorted_by_%s' % name add_method(Node, sort_method_name, sort_method, settings) match_method_name = 'is_%s' % name diff --git a/hyde/fs.py b/hyde/fs.py index 1b0fd03..f69f9e9 100644 --- a/hyde/fs.py +++ b/hyde/fs.py @@ -7,17 +7,17 @@ for common operations to provide a single interface. """ import codecs -import logging -from logging import NullHandler import mimetypes import os import shutil from distutils import dir_util import functools -# pylint: disable-msg=E0611 -logger = logging.getLogger('fs') -logger.addHandler(NullHandler()) +from hyde.logging import getLoggerWithNullHandler + +logger = getLoggerWithNullHandler('fs') + +# pylint: disable-msg=E0611 __all__ = ['File', 'Folder'] diff --git a/hyde/generator.py b/hyde/generator.py index 77521d8..5ea99f8 100644 --- a/hyde/generator.py +++ b/hyde/generator.py @@ -8,11 +8,8 @@ from hyde.template import Template from contextlib import contextmanager -import logging -from logging import NullHandler - -logger = logging.getLogger('hyde.engine') -logger.addHandler(NullHandler()) +from hyde.logging import getLoggerWithNullHandler +logger = getLoggerWithNullHandler('hyde.engine') class Generator(object): diff --git a/hyde/loader.py b/hyde/loader.py index 522a223..5beb9ca 100644 --- a/hyde/loader.py +++ b/hyde/loader.py @@ -5,10 +5,8 @@ import sys from hyde.exceptions import HydeException -import logging -from logging import NullHandler -logger = logging.getLogger('hyde.engine') -logger.addHandler(NullHandler()) +from hyde.logging import getLoggerWithNullHandler +logger = getLoggerWithNullHandler('hyde.engine') plugins = {} templates = {} diff --git a/hyde/logging.py b/hyde/logging.py new file mode 100644 index 0000000..64d191c --- /dev/null +++ b/hyde/logging.py @@ -0,0 +1,25 @@ +""" +Module for python 2.6 compatibility. +""" +import logging + +try: + from logging import NullHandler +except: + class NullHandler(logging.Handler): + """ + NOOP handler for libraries. + """ + def emit(self, record): + """ + /dev/null + """ + pass + +def getLoggerWithNullHandler(logger_name): + """ + Gets the logger initialized with the `logger_name` + and a NullHandler. + """ + logger = logging.getLogger(logger_name) + logger.addHandler(NullHandler()) diff --git a/hyde/plugin.py b/hyde/plugin.py index 9f02a63..823a0a4 100644 --- a/hyde/plugin.py +++ b/hyde/plugin.py @@ -5,6 +5,8 @@ Contains definition for a plugin protocol and other utiltities. import abc from hyde import loader +from hyde.logging import getLoggerWithNullHandler + class Plugin(object): """ The plugin protocol @@ -14,6 +16,8 @@ class Plugin(object): def __init__(self, site): super(Plugin, self).__init__() self.site = site + self.logger = getLoggerWithNullHandler(self.__class__.__name__) + def template_loaded(self, template): """ diff --git a/hyde/server.py b/hyde/server.py index 484a94f..b0292cd 100644 --- a/hyde/server.py +++ b/hyde/server.py @@ -12,7 +12,6 @@ from hyde.site import Site from hyde.generator import Generator import logging - logger = logging.getLogger('hyde.server') import sys diff --git a/hyde/site.py b/hyde/site.py index 46f4906..2cbc598 100644 --- a/hyde/site.py +++ b/hyde/site.py @@ -7,12 +7,11 @@ from hyde.exceptions import HydeException from hyde.fs import FS, File, Folder from hyde.model import Config -import logging -from logging import NullHandler -logger = logging.getLogger('hyde.engine') -logger.addHandler(NullHandler()) +from hyde.logging import getLoggerWithNullHandler +logger = getLoggerWithNullHandler('hyde.engine') + class Processable(object): """ A node or resource. diff --git a/hyde/template.py b/hyde/template.py index 801dbeb..7d5debb 100644 --- a/hyde/template.py +++ b/hyde/template.py @@ -5,6 +5,8 @@ Abstract classes and utilities for template engines """ from hyde.exceptions import HydeException +from hyde.logging import getLoggerWithNullHandler + class Template(object): """ Interface for hyde template engines. To use a different template engine, @@ -13,6 +15,7 @@ class Template(object): def __init__(self, sitepath): self.sitepath = sitepath + self.logger = getLoggerWithNullHandler(self.__class__.__name__) def configure(self, config): """