Skip to main content

Basic Features

  • Provide ltsvlogger.LTSVFormatter to format ltsv output.

  • Provide ltsvlogger.LTSVLoggerAdapter for ease to use.

Requirements

  • Python 2.6, 2.7, 3.2, 3.3, 3.4, 3.5, pypy.

Installation

Recommend: use virtualenv for this procedure:

$ pip install ltsvlogger

If you want to install unreleased version:

$ pip install https://bitbucket.org/shimizukawa/ltsvlogger/get/tip.zip

Using example

setup logger by code

import logging
from ltsvlogger import LTSVFormatter, LTSVLoggerAdapter

formatter = LTSVFormatter(fields={
    'asctime': 'time',
    'user': 'user',
    'host': 'host',
    'message': 'message',
})

logger = logging.getLogger('code')
hdr = logging.StreamHandler()
hdr.setLevel(logging.INFO)
hdr.setFormatter(formatter)
logger.addHandler(hdr)

# extra keyword argument values fill into format string placeholder.
# If formatter did not have 'user' and host' placeholder, these
# values will be simply omitted.
logger.error(
    'This is a error message with %s',
    'extra arguments',
    extra=dict(
        user='spam',
        host='ham.example.com',
    )
)

output sample:

host:ham.example.com\tmessage:This is a error message with extra arguments\tuser:spam\ttime:2013-09-27T09:21:03+00:00

If you want to output fields in order, you can setup formatter with fmt argument like logging.Formatter parameter:

formatter = LTSVFormatter(
    'time:%(asctime)s\tuser:%(user)s\thost:%(host)s\tmessage:%(message)s'
)

setup logger by code with LTSVLoggerAdapter

import logging
from ltsvlogger import LTSVFormatter, LTSVLoggerAdapter

formatter = LTSVFormatter()

logger = logging.getLogger('adapter')
hdr = logging.StreamHandler()
hdr.setLevel(logging.INFO)
hdr.setFormatter(formatter)
logger.addHandler(hdr)

# LTSVLoggerAdapter will extract keyword argument into log format.
ltsvlogger = LTSVLoggerAdapter(logger)

ltsvlogger.error(
    'This is a error message with %s',
    'keyword arguments',
     user='spam',
     host='ham.example.com',
)

output sample:

process_name:MainProcess\tlogger_name:sample\tthread_id:140654083024640\ttime:2013-09-27T08:49:53+00:00\tprocess_id:17807\tmessage:This is a error message with keyword arguments\thost:ham.example.com\tuser:spam\tthread_name:MainThread\tlog_level:ERROR

setup logger by config

Prepare logger.ini for logger:

[loggers]
keys = root,demo

[handlers]
keys = ltsvhdr

[formatters]
keys = ltsvfmt

[logger_root]
level = DEBUG
handlers =

[logger_demo]
level = DEBUG
handlers = ltsvhdr
qualname = demo

[handler_ltsvhdr]
class = StreamHandler
args = (sys.stderr,)
level = DEBUG
formatter = ltsvfmt

[formatter_ltsvfmt]
format = time:%(asctime)s\tlogger_name:%(name)s\tmessage:%(message)s
datefmt = %Y-%m-%dT%H:%M:%S%z
class = ltsvlogger.LTSVFormatter

and use:

import logging.config
logging.config.fileConfig('logger.ini')
logger = logging.getLogger('demo')

ltsvlogger = LTSVLoggerAdapter(logger)

ltsvlogger.warning(
    'This is a warning message with %s',
    'keyword arguments',
    user='spam',
    host='ham.example.com',
)

output sample:

time:2013-09-27T08:49:53+00:00\tlogger_name:demo\tmessage:This is a warning message with keyword arguments\thost:ham.example.com\tuser:spam

CHANGES

0.9.1 (2015-09-28)

Bug fixes.

  • PR#1. Fix typo. Thanks to WAKAYAMA shirou.

  • PR#2. Fix: remove redundant tab when no extra kwargs is passed. Thanks to Toshiya Kawasaki.

0.9.0 (2013-10-02)

First release.

  • Provide ltsvlogger.LTSVFormatter

  • Provide ltsvlogger.LTSVLoggerAdapter

Release files for ltsvlogger 0.9.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ltsvlogger 0.9.1
File Size Uploaded
ltsvlogger-0.9.1.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ltsvlogger 0.9.1
File Interpreter ABI Platform
ltsvlogger-0.9.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 11.2 kB

Release files / ltsvlogger-0.9.1.tar.gz

Download URL ltsvlogger-0.9.1.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e7f44a6ed4580614288bd50b2fdd940a9441b74d1f160e5da22379ba34469ba3
BLAKE2b-256 checksum
How to use checksums
bf9e98da2fdd4daf7b253c9d6ce584eeeba02d0003f2c5f83c8d1ef1243c7832
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / ltsvlogger-0.9.1-py2.py3-none-any.whl

Download URL ltsvlogger-0.9.1-py2.py3-none-any.whl
Size 6.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
e44c29766dfddd73405310811be8eee35639bb77b61c5b14d209971fe67593d9
BLAKE2b-256 checksum
How to use checksums
904e8859bef2f4517c723f742272f1894ec96037a0dc36b2c46ab541b8c0513f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.9.1 This release

2 release files

0.9.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page