Skip to content

Commit

Permalink
Add the capability to log to a file, as well as midnight rotation to …
Browse files Browse the repository at this point in the history
…acld.py
  • Loading branch information
0xxon committed Aug 5, 2016
1 parent 92115ee commit 6aba3bb
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions acld/acld.py
Expand Up @@ -16,6 +16,7 @@

from pybroker import *
from select import select
from logging.handlers import TimedRotatingFileHandler

def parseArgs():
defaultuser = os.getlogin()
Expand All @@ -30,6 +31,8 @@ def parseArgs():
parser.add_argument('--log-host', default=defaulthost, help='host name provided to acld (default: %(default)s)')
parser.add_argument('--topic', default="bro/event/pacf", help="Topic to subscribe to. Default: bro/event/pacf")
parser.add_argument('--debug', const=logging.DEBUG, default=logging.INFO, action='store_const', help="Enable debug output")
parser.add_argument('--logfile', help="Filename of logfile. If not given, logs to stdout")
parser.add_argument('--rotate', help="If logging to file and --rotate is specified, log will rotate at midnight", action="store_true")

args = parser.parse_args()
return args
Expand Down Expand Up @@ -303,8 +306,24 @@ def convert_element(self, el):
return None

args = parseArgs()
logging.basicConfig(level=args.debug,
format='%(created).6f:%(name)s:%(levelname)s:%(message)s')
logger = logging.getLogger('')
logger.setLevel(args.debug)

handler = None

if args.logfile:
if args.rotate:
handler = TimedRotatingFileHandler(args.logfile, 'midnight')
else:
handler = logging.FileHandler(args.logfile);
else:
handler = logging.StreamHandler(sys.stdout)

formatter = logging.Formatter('%(created).6f:%(name)s:%(levelname)s:%(message)s')
handler.setFormatter(formatter)

logger.addHandler(handler)

logging.info("Starting acld.py...")
brocon = Listen(args.topic, args.listen, int(args.port), args.acld_host, int(args.acld_port), args.log_user, args.log_host)
brocon.listen_loop()
Expand Down

0 comments on commit 6aba3bb

Please sign in to comment.