Logging¶
This module provides a easy to use and typed logging function for Nicotine+
Example
Using a python logging.Logger
from npc.logging import LOGGER
LOGGER.setLevel(logging.DEBUG)
LOGGER.debug("Hello World!")
Customizing the Logger
import logging
from npc.logging import NLogHandler
handler = NLogHandler()
format = logging.Formatter("%(levelname)s - %(message)s")
handler.setFormatter(format)
own_logger = logging.Logger("MyPlugin")
own_logger.addHandler(handler)
own_logger.info("Hello World!")
Using the log function
from npc.logging import log
log("Hello World!")
# Hello World!
log("Hello %s!", "World", prefix="MyPlugin")
# MyPlugin: Hello World!
log("Hello %s!", "World", title="MyPlugin")
# Opens a window with the title "MyPlugin" and the message "Hello World!"
- npc.LOGGER = <Logger npc (NOTSET)>¶
Your typical
logging.Loggerlogging to Nicotine+’s log facilityAdded in version 0.2.0.
It uses the
npc.logging.NLogHandlerto emit log recordsIt is already configured with the plugin’s name
It uses the format
%(name)s - %(levelname)s - %(message)s. Time is already added by Nicotine+
Example
from npc.logging import LOGGER LOGGER.setLevel(logging.DEBUG) LOGGER.debug("Hello World!")
- class npc.logging.NLogHandler¶
Custom logging handler for Nicotine+
Added in version 0.2.0.
This handler will emit log records using Nicotine+’s log facility.
Changed in version 0.3.5: Fix logging on Nicotine+ < 3.3.3
Example
import logging from npc.logging import NLogHandler # Create a handler handler = NLogHandler() # Set the formatter format = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(format) # Create a logger logger = logging.Logger("MyPlugin") logger.addHandler(handler) # Log a message logger.info("Hello World!")
See also
logging.Handlerfor more information on logging handlers- emit(record)¶
Emit a log record using Nicotine+’s log facility
- Parameters:
record (
logging.LogRecord) – Log record to be emitted
- npc.log(message, *message_args, level=LogLevel.DEFAULT, prefix=None, title=None, windowed=False, should_log_to_file=True)¶
Log a message to Nicotine’s log
Note
The log level is just a prefix defined by Nicotine+. It prepends the message with [LEVEL]. The prefix argument of this function is a custom prefix that will be added like so “PREFIX: message”. The reason as to why this prefix is provided instead of just using the level is because Nicotine+ filters out unknown log levels, thus no custom prefix would be shown.
Changed in version 0.3.5: Fix logging on Nicotine+ < 3.3.3
Changed in version 0.3.6: Fix windowed messages on Nicotine+ < 3.3.0
- Parameters:
message_args (
typing.Any) – Arguments to be formatted in the message. Common log python log arguments such as%s,%d, etc. can be usedlevel (
npc.types.LogLevel|npc.types.LegacyLogLevel, optional) – Log level, will be prefixed to the message in the format[LEVEL]: messageprefix (
str, optional) – Prefix to be added to the message, will be prefixed to the message in the format “PREFIX: message”title (
str, optional) – Title of the message, will force windowed to True. In N+ below 3.3.0 the title will be ignored while still showing the message in a window.windowed (
bool, optional) – Whether the message should be shown in a window. If title is not provided, it will be set to the name of the plugin. On N+ below 3.3.0, this will override the level tonpc.types.LegacyLogLevel.IMPORTANT_INFOunless the level isnpc.types.LegacyLogLevel.IMPORTANT_ERROR.should_log_to_file (
bool, optional) – Whether the message should be logged to the file