Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

prepare the publisher to use it as a library

  • Loading branch information...
commit 032c4a978aa6e94dfccbae8333eae5c1012a4a6f 1 parent d6693e4
Raphaël Vinot authored
Showing with 57 additions and 20 deletions.
  1. +57 −20 publisher.py
77 publisher.py
View
@@ -2,30 +2,67 @@
# -*- coding: utf-8 -*-
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.')
-parser.add_argument("-p", "--port", default=6379, type=int, help='Set the server port.')
-parser.add_argument("-c", "--channel",type=str, help='Channel to publish into.')
+def connect():
+ global hostname
+ 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):
- if i%2 == 0 :
- 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 info(message):
+ log('INFO', message)
+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)
Please sign in to comment.
Something went wrong with that request. Please try again.