fluent / fluent-logger-python

A structured logger for Fluentd (Python)
http://fluentd.org/
Other
444 stars 138 forks source link

Missing setLevel in the document #167

Closed chikinchoi closed 3 years ago

chikinchoi commented 4 years ago

Hi,

I am developing the fluent-logger-python in AWS Lambda. I found that I could not send logs to fluentd if I missing "log.setLevel(logging.DEBUG)". and the document didn't mention the statement too. Therefore, I am wondering whether the document missing this code.

Below is my lambda code:

import logging
from fluent import handler
def lambda_handler(event, context):
    function_name = context.function_name
    print(function_name)
    custom_format = {
        'host': '%(hostname)s',
        'where': '%(module)s.%(funcName)s',
        'type': '%(levelname)s',
        'stack_trace': '%(exc_text)s',
        'context': context,
        'function_name': function_name
    }
    logging.basicConfig(level=logging.INFO)
    log = logging.getLogger('fluent.test')
    log.setLevel(logging.DEBUG)
    h = handler.FluentHandler('lambdaPython.0434', host='******************', port=24224)
    formatter = handler.FluentRecordFormatter(custom_format)
    h.setFormatter(formatter)
    log.addHandler(h)
    log.info("This log entry will be logged2.")
arcivanov commented 4 years ago

Hmm, I don't think this has anything to do with Fluent logger, simply Python logging facilities. The log you're creating isn't a fluent logger object and its behavior is not controlled by this library at all. I'll investigate when I have time.

https://docs.python.org/3/library/logging.html#logging.Logger.setLevel

When a logger is created, the level is set to NOTSET (which causes all messages to be processed when the logger is the root logger, or delegation to the parent when the logger is a non-root logger). Note that the root logger is created with level WARNING.

The term ‘delegation to the parent’ means that if a logger has a level of NOTSET, its chain of ancestor loggers is traversed until either an ancestor with a level other than NOTSET is found, or the root is reached.

If an ancestor is found with a level other than NOTSET, then that ancestor’s level is treated as the effective level of the logger where the ancestor search began, and is used to determine how a logging event is handled.

If the root is reached, and it has a level of NOTSET, then all messages will be processed. Otherwise, the root’s level will be used as the effective level.
kenhys commented 3 years ago

It seems that non fluent-logger-python issue. I'll close it.