|
| 1 | +# Copyright 2010 New Relic, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 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 | +import logging |
| 16 | + |
| 17 | +from newrelic.api.application import application_instance |
| 18 | +from newrelic.api.transaction import current_transaction, record_log_event |
| 19 | +from newrelic.common.object_wrapper import wrap_function_wrapper |
| 20 | +from newrelic.core.config import global_settings |
| 21 | +from newrelic.hooks.logger_logging import add_nr_linking_metadata |
| 22 | +from newrelic.packages import six |
| 23 | + |
| 24 | +_logger = logging.getLogger(__name__) |
| 25 | + |
| 26 | +def loguru_version(): |
| 27 | + from loguru import __version__ |
| 28 | + return tuple(int(x) for x in __version__.split(".")) |
| 29 | + |
| 30 | + |
| 31 | +def _nr_log_forwarder(message_instance): |
| 32 | + transaction = current_transaction() |
| 33 | + record = message_instance.record |
| 34 | + message = record.get("_nr_original_message", record["message"]) |
| 35 | + |
| 36 | + if transaction: |
| 37 | + settings = transaction.settings |
| 38 | + else: |
| 39 | + settings = global_settings() |
| 40 | + |
| 41 | + # Return early if application logging not enabled |
| 42 | + if settings and settings.application_logging and settings.application_logging.enabled: |
| 43 | + level = record["level"] |
| 44 | + level_name = "UNKNOWN" if not level else (level.name or "UNKNOWN") |
| 45 | + |
| 46 | + if settings.application_logging.metrics and settings.application_logging.metrics.enabled: |
| 47 | + if transaction: |
| 48 | + transaction.record_custom_metric("Logging/lines", {"count": 1}) |
| 49 | + transaction.record_custom_metric("Logging/lines/%s" % level_name, {"count": 1}) |
| 50 | + else: |
| 51 | + application = application_instance(activate=False) |
| 52 | + if application and application.enabled: |
| 53 | + application.record_custom_metric("Logging/lines", {"count": 1}) |
| 54 | + application.record_custom_metric("Logging/lines/%s" % level_name, {"count": 1}) |
| 55 | + |
| 56 | + if settings.application_logging.forwarding and settings.application_logging.forwarding.enabled: |
| 57 | + try: |
| 58 | + record_log_event(message, level_name, int(record["time"].timestamp())) |
| 59 | + except Exception: |
| 60 | + pass |
| 61 | + |
| 62 | + |
| 63 | +ALLOWED_LOGURU_OPTIONS_LENGTHS = frozenset((8, 9)) |
| 64 | + |
| 65 | +def bind_log(level_id, static_level_no, from_decorator, options, message, args, kwargs): |
| 66 | + assert len(options) in ALLOWED_LOGURU_OPTIONS_LENGTHS # Assert the options signature we expect |
| 67 | + return level_id, static_level_no, from_decorator, list(options), message, args, kwargs |
| 68 | + |
| 69 | + |
| 70 | +def wrap_log(wrapped, instance, args, kwargs): |
| 71 | + try: |
| 72 | + level_id, static_level_no, from_decorator, options, message, subargs, subkwargs = bind_log(*args, **kwargs) |
| 73 | + options[-2] = nr_log_patcher(options[-2]) |
| 74 | + except Exception as e: |
| 75 | + _logger.debug("Exception in loguru handling: %s" % str(e)) |
| 76 | + return wrapped(*args, **kwargs) |
| 77 | + else: |
| 78 | + return wrapped(level_id, static_level_no, from_decorator, options, message, subargs, subkwargs) |
| 79 | + |
| 80 | + |
| 81 | +def nr_log_patcher(original_patcher=None): |
| 82 | + def _nr_log_patcher(record): |
| 83 | + if original_patcher: |
| 84 | + record = original_patcher(record) |
| 85 | + |
| 86 | + transaction = current_transaction() |
| 87 | + |
| 88 | + if transaction: |
| 89 | + settings = transaction.settings |
| 90 | + else: |
| 91 | + settings = global_settings() |
| 92 | + |
| 93 | + if settings and settings.application_logging and settings.application_logging.enabled: |
| 94 | + if settings.application_logging.local_decorating and settings.application_logging.local_decorating.enabled: |
| 95 | + record["_nr_original_message"] = message = record["message"] |
| 96 | + record["message"] = add_nr_linking_metadata(message) |
| 97 | + |
| 98 | + if loguru_version() > (0, 6, 0): |
| 99 | + if original_patcher is not None: |
| 100 | + patchers = [p for p in original_patcher] # Consumer iterable into list so we can modify |
| 101 | + # Wipe out reference so patchers aren't called twice, as the framework will handle calling other patchers. |
| 102 | + original_patcher = None |
| 103 | + else: |
| 104 | + patchers = [] |
| 105 | + |
| 106 | + patchers.append(_nr_log_patcher) |
| 107 | + return patchers |
| 108 | + else: |
| 109 | + return _nr_log_patcher |
| 110 | + |
| 111 | + |
| 112 | +def wrap_Logger_init(wrapped, instance, args, kwargs): |
| 113 | + result = wrapped(*args, **kwargs) |
| 114 | + patch_loguru_logger(instance) |
| 115 | + return result |
| 116 | + |
| 117 | + |
| 118 | +def patch_loguru_logger(logger): |
| 119 | + if hasattr(logger, "_core"): |
| 120 | + if not hasattr(logger._core, "_nr_instrumented"): |
| 121 | + logger.add(_nr_log_forwarder, format="{message}") |
| 122 | + logger._core._nr_instrumented = True |
| 123 | + elif not hasattr(logger, "_nr_instrumented"): |
| 124 | + for _, handler in six.iteritems(logger._handlers): |
| 125 | + if handler._writer is _nr_log_forwarder: |
| 126 | + logger._nr_instrumented = True |
| 127 | + return |
| 128 | + |
| 129 | + logger.add(_nr_log_forwarder, format="{message}") |
| 130 | + logger._nr_instrumented = True |
| 131 | + |
| 132 | + |
| 133 | +def instrument_loguru_logger(module): |
| 134 | + if hasattr(module, "Logger"): |
| 135 | + wrap_function_wrapper(module, "Logger.__init__", wrap_Logger_init) |
| 136 | + if hasattr(module.Logger, "_log"): |
| 137 | + wrap_function_wrapper(module, "Logger._log", wrap_log) |
| 138 | + |
| 139 | + |
| 140 | +def instrument_loguru(module): |
| 141 | + if hasattr(module, "logger"): |
| 142 | + patch_loguru_logger(module.logger) |
0 commit comments