Skip to main content

ulogger

Build Status Test Coverage

A micro Python logging library.

Supported handlers:

Requirements

  • Python 3.7. Tests also pass on Python 3.8. PyPy support has been removed due to upstream issues with the grpcio module.
  • Support for Linux & OS X

To Use

```sh
(env) $ pip install ulogger
# To use the stackdriver handler, you need to specify an extra dependency:
(env) $ pip install "ulogger[stackdriver]"
```

```python
import logging
from ulogger import setup_logging

# one handler
setup_logging('my_program', 'INFO', ['syslog'])

# multiple handlers
setup_logging('my_program', 'INFO', ['syslog', 'stream'])

# use a different logging facility for syslog (default 16/LOG_LOCAL0)
setup_logging('my_program', 'INFO', ['syslog'], facility=1)
setup_logging('my_program', 'INFO', ['syslog'], facility=logging.handlers.SysLogHandler.LOG_USER)

# then log messages normally
logging.info('ohai')
```

To just setup a specific handler, e.g. Syslog:

```python
import logging
from ulogger import syslog

logger = logging.getLogger('my_logger')
handler = syslog.get_handler('my_program')
logger.addHandler(handler)
```

To setup a Syslog handler with a specific address:

```python
import logging
from ulogger import syslog

logger = logging.getLogger('my_logger')

syslog_addr = ('10.99.0.1', 9514)  # (host, port) tuple
# if just a host is given, the default port 514 is used
syslog_addr = ('localhost', None)  # (host, port)
# filepath is supported
syslog_addr = '/dev/log'

handler = syslog.get_handler('my_program', address=syslog_addr)

# env vars are also supported, but will be overwritten if `address` is explicitly given
os.environ['SYSLOG_HOST'] = 'localhost'
os.environ['SYSLOG_PORT'] = 325
handler = syslog.get_handler('my_program')

# TCP & UDP are supported
proto = 1  # TCP
proto = socket.SOCK_STREAM  # TCP
proto = 2  # UDP - default
proto = socket.SOCK_DGRAM  #  UDP - default

handler = syslog.get_handler('my_program', address=syslog_addr, proto=proto)
logger.addHandler(handler)
```

Formatting

Default

The default date format for all handlers is the following: '%Y-%m-%dT%H:%M:%S' (example 2017-11-02T09:51:33.792).

The default log format is slightly different depending on the handler you select:

Stream Handler Log Format
```python
'%(asctime)s.%(msecs)03dZ <PROGNAME> (%(process)d) %(levelname)s: %(message)s'
```

Example:

```text
2017-11-02T09:51:33.792Z my_awesome_program (63079) INFO: Beginning awesome program v3.
```
Syslog Handler Log Format on Linux
```python
'%(asctime)s.%(msecs)03dZ <PROGNAME> (%(process)d): %(message)s'
```

Example:

```text
2017-11-02T09:51:33.792Z my_awesome_program (63079): Beginning awesome program v3.
```
Syslog Handler Log Format on OS X
```python
'<PROGNAME> (%(process)d): %(message)s'
```

Example:

```text
Aug 25 13:00:51 my-host.example.net my_awesome_program (63079): Beginning awesome program v3.
```

NOTE: Default syslog on OS X appends the date and hostname to the log record.

Stackdriver Handler Log Format
```python
'%(asctime)s.%(msecs)03d <HOST> <PROGNAME> (%(process)d): %(message)s'
```

Example:

```text
2017-11-02T19:00:55.850 my-gcp-host my_awesome_program (63079): Beginning awesome program v3"
```

Custom

To add your custom log and/or date formatter:

```python
import logging
from ulogger import setup_logging

log_fmt = '%(created)f %(levelno)d %(message)s'
log_date_fmt = '%Y-%m-%dT%H:%M:%S'

setup_logging('my_program', 'INFO', ['syslog'], log_fmt, log_date_fmt)
```

Development

For development and running tests, your system must have all supported versions of Python installed. We suggest using pyenv.

Setup

```sh
$ git clone git@github.com:spotify/ulogger.git && cd ulogger
# make a virtualenv
(env) $ pip install -r dev-requirements.txt
```

Running tests

To run the entire test suite:

```sh
# outside of the virtualenv
# if tox is not yet installed
$ pip install tox
$ tox
```

If you want to run the test suite for a specific version of Python:

```sh
# outside of the virtualenv
$ tox -e py37
```

To run an individual test, call pytest directly:

```sh
# inside virtualenv
(env) $ pytest tests/test_syslog.py
```

Code of Conduct

This project adheres to the Open Code of Conduct. By participating, you are expected to honor this code.

Metadata

Release files for ulogger 3.0.0

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

Source distribution (sdist)

Source distribution for ulogger 3.0.0
File Size Uploaded Provenance
ulogger-3.0.0.tar.gz 17.6 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 32.5 kB

Release files / ulogger-3.0.0.tar.gz

Download URL ulogger-3.0.0.tar.gz
Size 17.6 kB
Tags Source
SHA-256 checksum
How to use checksums
7ef36641d429efb406afb3bdbfa9f75000d9ae87c2ede044ac6059f99fc512b6
BLAKE2b-256 checksum
How to use checksums
c814f680f77d277050bc223d6a46b4f7d6a9d9ae5807f0d03ae8eb112a3df223
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / ulogger-3.0.0-py2.py3-none-any.whl

Download URL ulogger-3.0.0-py2.py3-none-any.whl
Size 15.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
3cc570e86a19546a01e154c0682bf3a8e38a97e085996acd9c01bf88b17dc442
BLAKE2b-256 checksum
How to use checksums
27fd22cf870147ac302500880c132633817c405ca05e1a8db2e68b853cf1c343
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Security for ulogger 3.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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