Superskyyy commented on code in PR #276: URL: https://github.com/apache/skywalking-python/pull/276#discussion_r1097649867
########## skywalking/plugins/sw_loguru.py: ########## @@ -0,0 +1,224 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import logging +import sys +import traceback +from multiprocessing import current_process +from os.path import basename, splitext +from threading import current_thread + +from skywalking import config, agent +from skywalking.protocol.common.Common_pb2 import KeyStringValuePair +from skywalking.protocol.logging.Logging_pb2 import LogData, LogDataBody, TraceContext, LogTags, TextLog +from skywalking.trace.context import get_context +from skywalking.utils.exception import IllegalStateError +from skywalking.utils.filter import sw_filter + +link_vector = ['https://pypi.org/project/loguru/'] +support_matrix = { + 'loguru': { + '>=3.7': ['0.6.0'] + } +} +note = """""" + + +def install(): + from loguru import logger + from loguru._recattrs import RecordException, RecordFile, RecordLevel, RecordProcess, RecordThread + from loguru._datetime import aware_now + from loguru._get_frame import get_frame + from loguru._logger import start_time, context as logger_context, Logger + from types import MethodType + + _log = logger._log + log_reporter_level = logging.getLevelName(config.log_reporter_level) # type: int + + def gen_record(self, level_id, static_level_no, from_decorator, options, message, args, kwargs): + """ Generate log record as loguru.logger._log """ + core = self._core + + if not core.handlers: + return + + (exception, depth, record, lazy, colors, raw, capture, patcher, extra) = options + + frame = get_frame(depth + 2) + + try: + name = frame.f_globals['__name__'] + except KeyError: + name = None + + try: + if not core.enabled[name]: + return + except KeyError: + enabled = core.enabled + if name is None: + status = core.activation_none + enabled[name] = status + if not status: + return + else: + dotted_name = name + '.' + for dotted_module_name, status in core.activation_list: + if dotted_name[: len(dotted_module_name)] == dotted_module_name: + if status: + break + enabled[name] = False + return + enabled[name] = True + + current_datetime = aware_now() + + if level_id is None: + level_icon = ' ' + level_no = static_level_no + level_name = f'Level {level_no}' # not really level name, just as loguru + else: + level_name, level_no, _, level_icon = core.levels[level_id] + + if level_no < core.min_level: + return + + code = frame.f_code + file_path = code.co_filename + file_name = basename(file_path) + thread = current_thread() + process = current_process() + elapsed = current_datetime - start_time + + if exception: + if isinstance(exception, BaseException): + type_, value, traceback = (type(exception), exception, exception.__traceback__) + elif isinstance(exception, tuple): + type_, value, traceback = exception + else: + type_, value, traceback = sys.exc_info() + exception = RecordException(type_, value, traceback) + else: + exception = None + + log_record = { + 'elapsed': elapsed, + 'exception': exception, + 'extra': {**core.extra, **logger_context.get(), **extra}, + 'file': RecordFile(file_name, file_path), + 'function': code.co_name, + 'level': RecordLevel(level_name, level_no, level_icon), + 'line': frame.f_lineno, + 'message': str(message), + 'module': splitext(file_name)[0], + 'name': name, + 'process': RecordProcess(process.ident, process.name), + 'thread': RecordThread(thread.ident, thread.name), + 'time': current_datetime, + } + + if capture and kwargs: + log_record['extra'].update(kwargs) + + if record: + kwargs.update(record=log_record) + + if args or kwargs: + log_record['message'] = message.format(*args, **kwargs) + + if core.patcher: + core.patcher(log_record) + + if patcher: + patcher(log_record) + + return log_record + + def _sw_log(self, level_id, static_level_no, from_decorator, options, message, args, kwargs): + _log(level_id, static_level_no, from_decorator, options, message, args, kwargs) + record = gen_record(self, level_id, static_level_no, from_decorator, options, message, args, kwargs) Review Comment: NoneType bug here, your gen_record function can return None if core handler missing (from `logger.remove()`), then it will error line 156 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
