add logging

This commit is contained in:
jb-alvarado 2018-01-10 15:41:56 +01:00
parent 4ea964e2d3
commit f3c3f2ded8
2 changed files with 62 additions and 2 deletions

View File

@ -29,6 +29,14 @@ sender_pass = 12345
recipient =
# Logging to file
# path to /var/log/ only if you run this program as deamon
# log_level can be: INFO, DEBUG, WARNING
[LOGGING]
log_file = /tmp/ffplayout.log
log_level = INFO
# output settings for the pre-compression
# all clips get prepared in that way,
# so the input for the final compression is unique

View File

@ -19,12 +19,16 @@
import configparser
import logging
import re
import smtplib
import sys
from argparse import ArgumentParser
from ast import literal_eval
from datetime import datetime, date, timedelta
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from logging.handlers import TimedRotatingFileHandler
from pathlib import Path
from shutil import copyfileobj
from subprocess import Popen, PIPE
@ -51,6 +55,11 @@ _mail = SimpleNamespace(
recip=cfg.get('MAIL', 'recipient')
)
_log = SimpleNamespace(
path=cfg.get('LOGGING', 'log_file'),
level=cfg.get('LOGGING', 'log_level')
)
_pre_comp = SimpleNamespace(
w=cfg.getint('PRE_COMPRESS', 'width'),
h=cfg.getint('PRE_COMPRESS', 'height'),
@ -95,6 +104,49 @@ else:
_playout.filter = []
# ------------------------------------------------------------------------------
# logging
# ------------------------------------------------------------------------------
stdin_parser = ArgumentParser(description="python and ffmpeg based playout")
stdin_parser.add_argument(
"-l", "--log", help="file to write log to (default '" + _log.path + "')"
)
# If the log file is specified on the command line then override the default
stdin_args = stdin_parser.parse_args()
if stdin_args.log:
_log.path = stdin_args.log
logger = logging.getLogger(__name__)
logger.setLevel(_log.level)
handler = TimedRotatingFileHandler(_log.path, when="midnight", backupCount=5)
formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
# capture stdout and sterr in the log
class ffplayout_logger(object):
def __init__(self, logger, level):
self.logger = logger
self.level = level
def write(self, message):
# Only log if there is a message (not just a new line)
if message.rstrip() != "":
self.logger.log(self.level, message.rstrip())
def flush(self):
pass
# Replace stdout with logging to file at INFO level
sys.stdout = ffplayout_logger(logger, logging.INFO)
# Replace stderr with logging to file at ERROR level
sys.stderr = ffplayout_logger(logger, logging.ERROR)
# ------------------------------------------------------------------------------
# global helper functions
# ------------------------------------------------------------------------------
@ -135,7 +187,7 @@ def send_mail(message, path):
server.sendmail(_mail.s_addr, _mail.recip, text)
server.quit()
else:
print('{}\n{}\n'.format(message, path))
logger.info('{}\n{}\n'.format(message, path))
# calculating the size for the buffer in bytes
@ -293,7 +345,7 @@ def play_clips(out_file):
else:
tm_str = str(timedelta(seconds=int(last_time)))
print('[{}] current play command:\n{}\n'.format(tm_str, src_cmd))
logger.info('play at "{}": {}'.format(tm_str, src_cmd))
filePiper = Popen(
[