Initial commit
[ta/rpmbuilder.git] / rpmbuilder / log.py
1 # Copyright 2019 Nokia
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """
16 Logging configuration for rpm builder
17 """
18 import logging
19 import os
20 import sys
21
22 def configure_logging(verbose=False, debugfile="debug.log"):
23     """ Logging to screen(console) and file """
24
25     debugfile_dirname = os.path.dirname(debugfile)
26     if not os.path.isdir(debugfile_dirname):
27         os.mkdir(debugfile_dirname)
28
29     logging.basicConfig(level=logging.DEBUG,
30                         format='%(asctime)s %(levelname)s: %(message)s',
31                         filename=debugfile,
32                         filemode='w')
33     # define a Handler which writes INFO messages or higher to the sys.stderr
34     console = logging.StreamHandler(stream=sys.stdout)
35     if verbose:
36         console.setLevel(logging.DEBUG)
37     else:
38         console.setLevel(logging.INFO)
39     # set a format which is simpler for console use
40     formatter = logging.Formatter('%(levelname)s: %(message)s')
41     # tell the handler to use this format
42     console.setFormatter(formatter)
43     # add the handler to the root log
44     logging.getLogger('').addHandler(console)