Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

logging.Logger

A logger receives messages, applies its threshold and sends accepted records to handlers.

Version: Python 3.14; repository image, production runtime not attested

Syntax

logging.getLogger('work.sample')

A logger receives messages, applies its threshold and sends accepted records to handlers.

A named logger normally propagates to ancestors. Disable propagation for a private handler when duplicate delivery would be unwanted.

Create named Logger instances through getLogger. The originating logger checks its effective level and filters. With propagate=True an accepted record goes directly to ancestor handlers, bypassing ancestor logger levels and filters; each handler can still filter the record. The sample uses a private handler and prints True ready. disabled=True on the originating logger prevents its calls from emitting records. The same flag on an ancestor does not block delivery of an accepted child record to the ancestor's handlers.

import logging,io
out=io.StringIO()
parent=logging.getLogger('ref.logger')
parent.propagate=False
parent.setLevel(logging.CRITICAL)
handler=logging.StreamHandler(out)
handler.setLevel(logging.WARNING)
parent.addHandler(handler)
child=logging.getLogger('ref.logger.child')
child.setLevel(logging.INFO)
child.info('filtered')
child.warning('delivered')
assert out.getvalue()=='delivered\n'
child.propagate=False
child.addHandler(logging.NullHandler())
child.warning('private')
assert out.getvalue()=='delivered\n'
child.propagate=True
parent.disabled=True
child.warning('child')
child.disabled=True
child.warning('blocked')
assert out.getvalue().splitlines()==['delivered','child']

logging.Logger.setLevel · logging.Logger.addHandler · logging.Logger.propagate · logging.Logger.getChild · logging.Logger.debug · logging.Logger.info · logging.Logger.warning · logging.Logger.error · logging.Logger.handlers

setLevel sets the logger threshold, addHandler installs a handler in handlers, and getChild('jobs') obtains a child logger. Treat handlers as read-only and change it with addHandler/removeHandler. debug/info/warning/error use levels 10/20/30/40. The child inherits INFO and passes three records to its parent, whose propagate=False stops further propagation.

import io,logging
out=io.StringIO()
log=logging.getLogger('app')
log.setLevel(logging.INFO)
log.propagate=False
h=logging.StreamHandler(out)
log.addHandler(h)
child=log.getChild('jobs')
assert child.name=='app.jobs' and h in log.handlers
for method in (child.debug,child.info,child.warning,child.error):
    method(method.__name__)
assert out.getvalue().splitlines()==['info','warning','error']
log.removeHandler(h)
h.close()

Examples

import logging
from io import StringIO

out = StringIO()
log = logging.getLogger("private")
log.setLevel(logging.INFO)
log.propagate = False
log.addHandler(logging.StreamHandler(out))
log.info("ready")
print(isinstance(log, logging.Logger), out.getvalue().strip())

Result

True ready

Create named Logger instances through getLogger. The originating logger checks its effective level and filters. With propagate=True an accepted record goes directly to ancestor handlers, bypassing ancestor logger levels and filters; each handler can still filter the record. The sample uses a private handler and prints True ready. disabled=True on the originating logger prevents its calls from emitting records. The same flag on an ancestor does not block delivery of an accepted child record to the ancestor's handlers.

Sources