Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #27 from myoung34/file_logging
Browse files Browse the repository at this point in the history
Add ability to log to a file
  • Loading branch information
myoung34 authored Sep 8, 2020
2 parents 1371ec5 + cbc80b3 commit 998e83b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ $ cat <<EOF >config.ini
[general]
sleep_interval = 2 # defaults to 1
logging_level = DEBUG # defaults to INFO
logfile = /var/log/foo.log # defaults to stdout
# stdout example
[stdout]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ def test_cli_no_params_success(
runner = CliRunner()
result = runner.invoke(cli.run, [])
assert result.exit_code == 0
assert "Scanning for Tilt data...\n{'color': 'Black', 'gravity': 1.053, 'temp': 60, 'mac': '00:0a:95:9d:68:16', 'timestamp'" in result.output# noqa
assert result.output == 'Scanning for Tilt data...\n'
8 changes: 7 additions & 1 deletion tilty/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def scan_and_emit(device: tilt_device.TiltDevice, emitters: List[dict]):
tilt_data = device.scan_for_tilt_data()
if tilt_data:
LOGGER.debug('tilt data retrieved')
click.echo(tilt_data)
LOGGER.info(tilt_data)
emit(emitters=emitters, tilt_data=tilt_data)
else:
LOGGER.debug('No tilt data')
Expand Down Expand Up @@ -107,12 +107,18 @@ def run(

CONFIG.read(config_file)

handler = logging.StreamHandler(sys.stdout)
logging_level = 'INFO'
try:
logging_level = CONFIG['general'].get('logging_level', 'INFO')
logfile = CONFIG['general'].get('logfile', None)
if logfile:
handler = logging.FileHandler(filename=logfile)
except KeyError:
pass
LOGGER.setLevel(logging.getLevelName(logging_level))
handler.setLevel(logging_level)
LOGGER.addHandler(handler)

device = tilt_device.TiltDevice()
signal.signal(signal.SIGINT, partial(terminate_process, device))
Expand Down
7 changes: 1 addition & 6 deletions tilty/tilty.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@
""" Class to encapsulate all the emitter logic """
import configparser
import logging
import sys
from typing import Any, List

from tilty.exceptions import ConfigurationFileEmptyException

LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
LOGGER.addHandler(handler)
LOGGER = logging.getLogger()


def parse_config(config: configparser.ConfigParser) -> List[dict]:
Expand Down

0 comments on commit 998e83b

Please sign in to comment.