Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:58:22

0001 #!/usr/bin/env python
0002 #
0003 # Licensed under the Apache License, Version 2.0 (the "License");
0004 # You may not use this file except in compliance with the License.
0005 # You may obtain a copy of the License at
0006 # http://www.apache.org/licenses/LICENSE-2.0OA
0007 #
0008 # Authors:
0009 # - Wen Guan, <wen.guan@cern.ch>, 2022
0010 
0011 
0012 import logging
0013 import os
0014 import sys
0015 
0016 from idds.common.config import (config_has_section, config_has_option,
0017                                 config_get)
0018 
0019 
0020 def setup_logging(name, stream=None, loglevel=None):
0021     """
0022     Setup logging
0023     """
0024     if loglevel is None:
0025         if config_has_section('common') and config_has_option('common', 'loglevel'):
0026             loglevel = getattr(logging, config_get('common', 'loglevel').upper())
0027         else:
0028             loglevel = logging.INFO
0029 
0030     if stream is None:
0031         if config_has_section('common') and config_has_option('common', 'logdir'):
0032             logging.basicConfig(filename=os.path.join(config_get('common', 'logdir'), name),
0033                                 level=loglevel,
0034                                 format='%(asctime)s\t%(threadName)s\t%(name)s\t%(levelname)s\t%(message)s')
0035         else:
0036             logging.basicConfig(stream=sys.stdout, level=loglevel,
0037                                 format='%(asctime)s\t%(threadName)s\t%(name)s\t%(levelname)s\t%(message)s')
0038     else:
0039         logging.basicConfig(stream=stream, level=loglevel,
0040                             format='%(asctime)s\t%(threadName)s\t%(name)s\t%(levelname)s\t%(message)s')
0041 
0042 
0043 def get_logger(name, filename=None, loglevel=None):
0044     """
0045     Setup logging
0046     """
0047     if loglevel is None:
0048         if config_has_section('common') and config_has_option('common', 'loglevel'):
0049             loglevel = getattr(logging, config_get('common', 'loglevel').upper())
0050         else:
0051             loglevel = logging.INFO
0052 
0053     if filename is None:
0054         filename = name + ".log"
0055     if not filename.startswith("/"):
0056         logdir = None
0057         if config_has_section('common') and config_has_option('common', 'logdir'):
0058             logdir = config_get('common', 'logdir')
0059         if not logdir:
0060             logdir = '/var/log/idds'
0061         filename = os.path.join(logdir, filename)
0062 
0063     formatter = logging.Formatter('%(asctime)s\t%(threadName)s\t%(name)s\t%(levelname)s\t%(message)s')
0064 
0065     handler = logging.FileHandler(filename)
0066     handler.setFormatter(formatter)
0067     logger = logging.getLogger(name)
0068     logger.setLevel(loglevel)
0069     logger.addHandler(handler)
0070     logger.propagate = False
0071     return logger
0072 
0073 
0074 def test_get_logger():
0075     logger = get_logger('test', filename='/tmp/wguan/test.log')
0076     logger.info("test")
0077     logger.debug("test1")
0078     print(logger.handlers)
0079 
0080 
0081 if __name__ == '__main__':
0082     setup_logging('test1')
0083     test_get_logger()