Coverage for src/radix_tree/radix_config.py: 94%

1 import logging

2 import logging.handlers

3 import sys

4

5# Log configuration --------------------------------------

6 LOG_FILENAME = '/tmp/radixTree.out'

7

8 LEVELS = {'debug': logging.DEBUG,

9 'info': logging.INFO,

10 'warning': logging.WARNING,

11 'error': logging.ERROR,

12 'critical': logging.CRITICAL}

13

14

15 def get_params(param_name):

16 """

17 Detection of an argument on the command line like <param_name>=<value>

18 Return <value> if any or ''

19 Example :

20 level=debug

21 01234567890

22 ^

23 p_name=5

24 """

25 p_name = len(param_name)

26 ret = ''

27 if len(sys.argv)>1:

28 for ar in range(len(sys.argv)):

29 arg = sys.argv[ar]

30 # print("arg : %s" % arg)

31 a = len(arg)

32 if a > p_name:

33 # print("arg[p_name] : %s" %arg[p_name])

34 # print("arg[0:p_name-1] : %s" %arg[0:p_name])

35 if arg[p_name] == '=' and arg[0: p_name] == param_name:

36 ret = arg[p_name+1:]

37 # print("Return value : %s" %ret)

38 return ret

39

40 level = logging.NOTSET # Par défaut, pas de log

41

42 level_name = get_params('level')

43 if level_name:

44 level = LEVELS.get(level_name, logging.NOTSET)

45

46 my_logger = logging.getLogger(__name__)

47 my_logger.setLevel(level)

48

49# Add the handler

50 handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=1048576, backupCount=5)

51# Formatter creation

52 formatter = logging.Formatter("%(asctime)s %(name)s %(levelname)s %(message)s")

53# Add formatter to handler

54 handler.setFormatter(formatter)

55 my_logger.addHandler(handler)

56

57 handler = logging.StreamHandler()

58# Formatter creation

59 formatter = logging.Formatter("%(asctime)s %(name)s %(levelname)s %(filename)s %(lineno)d %(message)s")

60# Add formatter to handler

61 handler.setFormatter(formatter)

62 my_logger.addHandler(handler)

63

64 print("Fin d'init...")

65# End log configuration --------------------------------------