Skip to content

Commit

Permalink
prepare the publisher to use it as a library
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafiot committed Sep 11, 2012
1 parent d6693e4 commit 032c4a9
Showing 1 changed file with 57 additions and 20 deletions.
77 changes: 57 additions & 20 deletions publisher.py
Expand Up @@ -2,30 +2,67 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-


import redis import redis
import argparse
import time


parser = argparse.ArgumentParser(description='Configure a logging publisher.') hostname = 'localhost'
port = 6379
channel = None
redis_instance = None


parser.add_argument("-H", "--hostname", default='localhost', type=str, help='Set the hostname of the server.') def connect():
parser.add_argument("-p", "--port", default=6379, type=int, help='Set the server port.') global hostname
parser.add_argument("-c", "--channel",type=str, help='Channel to publish into.') global port
global redis_instance
redis_instance = redis.StrictRedis(host=hostname, port=port)


args = parser.parse_args() def log(level, message):
global channel
if redis_instance is not None and channel is not None:
c = '{channel}.{level}'.format(channel = channel, level = level)
redis_instance.publish(c, message)


r = redis.StrictRedis(host=args.hostname, port=args.port, db=0) def debug(message):
log('DEBUG', message)


for i in range(0,21): def info(message):
if i%2 == 0 : log('INFO', message)
r.publish(args.channel + '.INFO', 'test' + str(i))
elif i%3 == 0 :
r.publish(args.channel + '.WARNING', 'test' + str(i))
elif i%5 == 0:
r.publish(args.channel + '.ERROR', 'test' + str(i))
elif i%7 == 0:
r.publish(args.channel + '.CRITICAL', 'test' + str(i))
else:
r.publish(args.channel + '.DEBUG', 'test' + str(i))


def warning(message):
log('WARNING', message)


time.sleep(1) def error(message):
log('ERROR', message)

def critical(message):
log('CRITICAL', message)



if __name__ == '__main__':
import argparse
import time

parser = argparse.ArgumentParser(description='Configure a logging publisher.')

parser.add_argument("-H", "--hostname", default='localhost', type=str, help='Set the hostname of the server.')
parser.add_argument("-p", "--port", default=6379, type=int, help='Set the server port.')
parser.add_argument("-c", "--channel",type=str, required=True, help='Channel to publish into.')

args = parser.parse_args()

channel = args.channel
hostname = args.hostname
port = args.port

connect()
for i in range(0,21):
if i%2 == 0 :
info('test' + str(i))
elif i%3 == 0 :
warning('test' + str(i))
elif i%5 == 0:
error('test' + str(i))
elif i%7 == 0:
critical('test' + str(i))
else:
debug('test' + str(i))
time.sleep(1)

0 comments on commit 032c4a9

Please sign in to comment.