Newer
Older
##############################################################################
#
# ICU Format, a module for Odoo
# Copyright © 2019-2021 XCG Consulting <https://xcg-consulting.fr>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import logging
from .icu_format import icu_format
# for the logging, logging.Formatter is not used because it only change the
# line formatting used, not the message formatting itself.
# https://docs.python.org/3/library/logging.html#logging.Formatter
# Using LoggerAdapter still allows % formatting, but it should be avoided
# as it means doing double formatting of the message.
class ICUFormatAdapter(logging.LoggerAdapter):
"""Adapter that will call :func:`icu_format` when there is something to
display.
Beware that if this is subclassed, the message processing for ICU is done
in the :func:`log` method, not in the process one.
.. warning:: do not use for translated logs, this uses the neutral locale
(C) to format the messages
"""
def log(self, level, msg, *args, **kwargs):
"""Changed to reformat the message according to the lang information,
and from the provided dictionary.
"""
if self.isEnabledFor(level):
if args:
args_dict = args[0]
args = args[1:]
else:
msg = icu_format("C", msg, args_dict)
msg, kwargs = self.process(msg, kwargs)
self.logger.log(level, msg, *args, **kwargs)
# In a logger, the method is called getChild but naming convention have
# changed, so usi get_child instead here.
def get_child(self, suffix):
"""
Get a logger which is a descendant to this one.
This is a convenience method, such that
getLogger('abc').getChild('def.ghi')
is the same as
getLogger('abc.def.ghi')
It's useful, for example, when the parent logger is named using
__name__ rather than a literal string.
"""
return ICUFormatAdapter(self.logger.getChild(suffix), self.extra)
def get_logger(name: str = None) -> logging.LoggerAdapter:
"""Provide a way to get a Logger that will call :func:`icu_format` only
when needed (to avoid spending time formatting a message that will not be
displayed). This is similar to what is done in logging for formatting.
.. code-block:: python
from odoo.addons.icuformat import get_logger
_logger = get_logger(__name__)
_logger.info("Simple message")
_logger.info("Value: {value, number}", {"value": 1000})
"""
logger = logging.getLogger(name)