Skip to main content

My logger library.

Project description

mylog

Codacy Badge CodeFactor pre-commit.ci status Build Status codecov Linting

A simple logging library for Python.

Example/Getting started

First, install it with pip install git+https://github.com/koviubi56/mylog.git. Need more help?

For demonstration purposes only! Output may not be the same as shown here!

import mylog

logger = mylog.root.get_child()

logger.info("Hello, world!")
# Returns: 0
# Output: <nothing>
# Why? Because the threshold is set to WARNING.

# Try this:
logger.warning("Hello, world!")
# Returns: 70
# Output: [WARNING Sun Mar 27 18:56:57 2022 line: 00001] Hello, world!

# Or
logger.threshold = mylog.Level.debug
logger.info("Hello, world!")
# Returns: 67
# Output: [INFO Sun Mar 27 18:58:40 2022 line: 00001] Hello, world!

API reference

Warning! This is only the public API!

WARNING! If you see a - (minus/dash/hyphen) in an argument name, it is a _ (underscore)!

variable __version__

The version string of the library.

constant DEFAULT_FORMAT

The default format used by MyLog. By default it is "[{lvl} {time} line: {line}] {indent}{msg}".

IntEnum class Level

Different levels of logging. (Note that logging levels aren't required to be one of these.)

Name Value
debug 10
info 20
warning 30
error 40
critical 50

Aliases:

  • warn == warning
  • fatal == critical

constant DEFAULT_THRESHOLD

The default threshold used by MyLog. By default it is Level.warning.

protocol Stringable

A class that has a __str__ method, that returns a string.

type alias Levelable

Union[Level, Stringable, int]

function to_level(lvl: Levelable, int-ok: bool = False) -> Level

Converts an int/str/something to a Level. If lvl cannot be converted, and int_ok is True, the int will be returned.

type alias NoneType

type(None)

context manager SetAttr(obj: object, name: str, new-value: Any)

A context manager for setting and then resetting an attribute.

class LogEvent(msg: str, level: Level, time: float, indent: int, frame-depth: int, tb: bool)

A log event. Time is in UNIX seconds.

protocol StreamProtocol

A class that has a write and a flush method.

ABC Handler

An abstract base class for that has a method handle.

class NoHandler

A handler that does nothing.

class StreamWriterHandler(stream: StreamProtocol, *, flush: bool = True, use-colors: bool = True, format-msg: bool = True)

A handler that writes to a stream.

class Logger(higher: Optional[Logger])

The logger class.

Class variables

bool colors

Should level_to_str use colors? Defaults to True.

Static methods

_color(rv: str) -> str

Colorizes a string.

Methods

_inherit() -> None

Inherit from self.higher ("parent").

get_default_handlers() -> List[Handler]

Get the default handlers.

level_to_str(lvl: Levelable) -> str

Convert a level to a string.

format_msg(event: LogEvent) -> str

Format the message.

_log(lvl: Levelable, msg: Stringable, tb: bool, frame-depth: int) -> Optional[LogEvent]

Log a message.

debug/info/warning/error/critical(msg: Stringable, traceback: bool = False) -> int

Log a message.

get_child() -> Logger

Get a child logger.

is_enabled_for(lvl: Levelable) -> bool

Check if the logger is enabled for the given level.

context manager IndentLogger(logger: Logger)

Indent the logger by one when entered, then unindent by one when exited.

context manager ChangeThreshold(logger: Logger, level: Levelable)

Change the threshold of the logger to level when entered, then restore the threshold when exited.

variable root

The root logger.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

GNU LGPLv3.0+

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-mylog-0.4.0.tar.gz (14.1 kB view hashes)

Uploaded Source

Built Distribution

python_mylog-0.4.0-py3-none-any.whl (12.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page