Skip to main content

Simple logger in python mainly used for CLI tool like argparse

Project description

# pyplelogger

[![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://github.com/ellerbrock/open-source-badges/)
[![Build Status](https://travis-ci.com/KeisukeYamashita/pylogger.svg?branch=master)](https://travis-ci.com/KeisukeYamashita/pylogger)
[![Python 3.3](https://img.shields.io/badge/python->3.3-blue.svg)](https://www.python.org/downloads/release/python-330/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

[![Maintainability](https://api.codeclimate.com/v1/badges/a489ad892561ae5ece20/maintainability)](https://codeclimate.com/github/KeisukeYamashita/pyplelogger/maintainability)
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/dwyl/esta/issues)

> Simple logger written in python mostly for command line interface tools.


## Install

```shell
pip install pyplelogger
```

## Usage

### Simple usage

This is the atomic usage. Import this and print it out.

```python
from pyplelogger import Logger

log = Logger(__name__).build()
log.info("hogehoge")

INFO 2018-11-26 23:11:15,109 test.py:main in line 4: hogehoge
```

The default log level is `INFO` and you have to pass unique name for each handler.

### Change default logger level

You can change logger level entire the project.

```python
import logging
from pyplelogger import Logger

Logger.set_default_log_level(logging.WARNING)

log = Logger(__name__).build()
log.info("hogehoge")

#=> Nothing is pritted out
```

And once you set teh default log level, it is valid in entire project.

The logger levels are defined in `logging` library.

| level | number |
|:----|:----|
| CRITICAL | 50 |
| ERROR | 40 |
| WARNING | 30 |
| INFO | 20 |
| DEBUG | 10 |
| NOTSET | 0 |

Let's say you have a `script1.py` and `script2.py`.

If you change defaul log level in `script1.py` like this,

```python
import logging
from pyplelogger import Logger

Logger.set_default_log_level(logging.WARNING)
```

it is valid in `script2.py` too.

```python
import logging
from pyplelogger import Logger

log = Logger(__name__).build()
log.info("hogehoge")

#=> Nothing is pritted out
```

### Change logger level

This is similier to changing default log level but this method changes log level for one logger.

Futhermore, the method to change default log level is a class method but, this method is a instance method, so it will only effect the instance.

```python
import logging
from pyplelogger import Logger

log = Logger(__name__).set_log_level(logging.WARNING).build()
log.info("hogehoge")

#=> Nothing is pritted out
```

### Change default format

You can change default format of every instance of Logger by this method. Pass string object describing logging format.

```python
format = '%(levelname)s %(asctime)s %(message)s'
Logger.set_default_format(format)

log = Logger(__name__).build()
log.info("hogehoge")
```

This will print out this logs.

```shell
INFO 2018-11-28 18:11:15,109 hogehoge
```

### Change format

Specify format in string. The default format is `'%(levelname)s %(asctime)s %(module)s.py:%(funcName)s in line %(lineno)d: %(message)s'`.

```python
from pyplelogger import Logger

log_before = Logger(__name__).build()
log_after = Logger(__name__ + "after").set_format('%(levelname)s %(message)s').build()

log_before.info("before")
log_after.info("after")
```

Here is the output.

```shell
INFO 2018-11-26 23:11:15,109 test.py:main in line 4: before
INFO after
```

## With Argparse

This has good integration with [argparse](https://docs.python.jp/3/library/argparse.html), a library for creating cli tool.

### Verbose flag

This is just a simple example of the verbose flag.

```python
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--verbose', '-v', action='count')

args = parser.parse_args()
```

Now you can get number of `v` flags like

- `-v`: 1
- `-vvv`: 3

You can convert to log level by Using `IncrementalLoggerLevel` IntEnum Class.

```python
# 1 is debug level

count = 1

log_level = IncrementalLoggerLevel.convert_logger_level(1)

log = Logger().set_log_level(log_level)
log.DEBUG("hoge")
```

Then you will see

```
#=>
DEBUG 2018-11-26 23:11:15,109 test.py:main in line 3: hoge
```

The default level of logging is `INFO` in current version.

| level | number | count |
|:----|:----|:---|
| CRITICAL | 50 | - |
| ERROR | 40 | - |
| WARNING | 30 | - |
| INFO | 20 | 0 |
| DEBUG | 10 | 1 |
| NOTSET | 0 | 2 |

## To contribute

We welcome for your contribution.

1. Fork this project
2. Run `just` to install dependencies.
3. Give us a pull request

## Member

- KeisukeYamashita: Maintainer and creater

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyplelogger-0.1.7-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file pyplelogger-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: pyplelogger-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.0

File hashes

Hashes for pyplelogger-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 fb607f226f2679c88ded5e7a2897f4dfe743d2c37c469734f6ab5ddfd1a580ea
MD5 1037a6e7ecfdf4e5690db7883592cf23
BLAKE2b-256 e2513c1b9dc3859f4da86e786b420e8976d41c0777ad4f4d7be1310f7cf1bc4f

See more details on using hashes here.

Supported by

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