Skip to content

Commit

Permalink
logger: move get_logger into tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Exirel committed Oct 8, 2019
1 parent 664813b commit 3eb872d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
46 changes: 10 additions & 36 deletions sopel/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
from logging.config import dictConfig

from .tools import deprecated
from sopel import tools


class IrcLoggingHandler(logging.Handler):
Expand Down Expand Up @@ -119,49 +119,23 @@ def setup_logging(settings):
dictConfig(logging_config)


@deprecated(
'Use `sopel.logger.get_plugin_logger` instead',
version='7.0',
removed_in='8.0')
def get_logger(name=None):
"""Return a logger for a module, if the name is given.
This is equivalent to ``logging.getLogger('sopel.modules.' + name)`` when
name is given, and ``logging.getLogger('sopel')`` when it is not.
The latter case is intended for use in Sopel's core; modules should call
``get_logger(__name__)`` to get a logger.
.. deprecated:: 7.0
Use ``logging.getLogger(__name__)`` in Sopel's code instead, and
:func:`get_plugin_logger` for external plugins.
:func:`sopel.tools.get_logger` for external plugins.
This will warn a deprecation warning in Sopel 8.0 then removed in 9.0.
"""
if name:
return logging.getLogger('sopel.modules.' + name)
else:
if not name:
return logging.getLogger('sopel')

parts = name.strip().split('.')
if len(parts) > 1 or parts[0] in ['sopel', 'sopel_modules']:
return logging.getLogger(name)

def get_plugin_logger(plugin_name):
"""Return a logger for a plugin.
:param str plugin_name: name of the plugin
:return: the logger for the given plugin
This::
from sopel import logger
LOGGER = logger.get_plugin_logger('my_custom_plugin')
is equivalent to this::
import logging
LOGGER = logging.getLogger('sopel.externals.my_custom_plugin')
Internally, Sopel configures logging for the ``sopel`` namespace, so
external plugins can't benefit from it with ``logging.getLogger(__name__)``
as they won't be in the same namespace. This function uses the
``plugin_name`` with a prefix inside this namespace.
"""
return logging.getLogger('sopel.externals.%s' % plugin_name)
# assume it's a plugin name, as intended by the original get_logger
return tools.get_logger(name)
25 changes: 25 additions & 0 deletions sopel/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import codecs
import functools
import logging
import os
import re
import sys
Expand Down Expand Up @@ -534,6 +535,30 @@ def get_hostmask_regex(mask):
return re.compile(mask + '$', re.I)


def get_logger(plugin_name):
"""Return a logger for a plugin.
:param str plugin_name: name of the plugin
:return: the logger for the given plugin
This::
from sopel import plugins
LOGGER = plugins.get_logger('my_custom_plugin')
is equivalent to this::
import logging
LOGGER = logging.getLogger('sopel.externals.my_custom_plugin')
Internally, Sopel configures logging for the ``sopel`` namespace, so
external plugins can't benefit from it with ``logging.getLogger(__name__)``
as they won't be in the same namespace. This function uses the
``plugin_name`` with a prefix inside this namespace.
"""
return logging.getLogger('sopel.externals.%s' % plugin_name)


class SopelMemory(dict):
"""A simple thread-safe ``dict`` implementation.
Expand Down

0 comments on commit 3eb872d

Please sign in to comment.