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.Logger logging to Nicotine+’s log facility

Added in version 0.2.0.

  • It uses the npc.logging.NLogHandler to emit log records

  • It 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.Handler for 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: