annotate TEisotools-1.0/commons/core/LoggerFactory.py @ 6:20ec0d14798e draft

Uploaded
author urgi-team
date Wed, 20 Jul 2016 05:00:24 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
1 # Copyright INRA (Institut National de la Recherche Agronomique)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
2 # http://www.inra.fr
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
3 # http://urgi.versailles.inra.fr
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
4 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
9 # "http://www.cecill.info".
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
10 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
15 # liability.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
16 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
26 # same conditions as regards security.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
27 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
30
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
31
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
32 ## @mainpage Documentation of the REPET API
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
33 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
34 # Welcome to the API documentation!
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
35 # This API is a set of packages and classes for pipeline(s) development.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
36 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
37 # @par The "logger" package
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
38 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
39 # Logging is managed via LoggerFactory. This class creates instances of logging.logging python class. It's strongly encouraged to use this factory each time you need to log something.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
40 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
41 # @par The "checker" package
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
42 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
43 # This package is a set of classes designed to facilitate development of different kind of checks: filesystem checks, environment checks, configuration file checks ...
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
44 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
45 # Classes should subclass checker::IChecker or if a logger is needed: checker::AbstractChecker.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
46 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
47 # Methods should raise checker::CheckerException.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
48 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
49 # Use checker::ConfigChecker and checker::ConfigException for configuration files checks.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
50 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
51 # checker::CheckerUtils is a set of small static methods shared by other classes of checker package.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
52 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
53 # @par The "coord" package
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
54 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
55 # This package is a set of classes dedicated to coordinates manipulations.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
56 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
57 # A coord::Range instance records a region on a given sequence (start, end and sequence name).
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
58 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
59 # A coord::Map instance is a coord::Range instance and record a named region on a given sequence (start, end, sequence name and name).
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
60 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
61 # A coord::Set instance is a coord::Map instance and record a named region on a given sequence with an identifier (start, end, sequence name, name and id).
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
62 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
63 # A coord::Align instance handle a match between two sequences, query and subject (pair of coordinates with E-value, score and identity).
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
64 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
65 # A coord::Path instance is a coord::Align instance and handle a match between two sequences, query and subject (pair of coordinates with E-value, score and identity) with an identifier.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
66 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
67 # A coord::Match instance is a coord::Path instance and handle a chain of match(es) between two sequences, query and subject, with an identifier and the length of the input sequences.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
68 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
69 # coord::Align, coord::Map, coord::Path and coord::Set come with utils classes: coord::AlignUtils, coord::MapUtils, coord::PathUtils and coord::SetUtils.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
70 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
71 # @par The "seq" package
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
72 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
73 # This package a set of classes dedicated to sequences manipulations.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
74 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
75 # A seq::Bioseq instance records a sequence with its header. seq::Bioseq comes with an utils class: seq::BioseqUtils.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
76 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
77 # A seq::BioseqDB instance handle a collection of a Bioseq (header-sequence).
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
78 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
79 # A seq::AlignedBioseqDB instance is a multiple sequence alignment representation.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
80 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
81 # A seq::FastaUtils is a set of static methods for fasta file manipulation.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
82 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
83 # @par The "sql" package
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
84 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
85 # This package is dedicated to persistance of coord package objects.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
86 # All classes come with dedicated interfaces. Use these interfaces for class manipulation.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
87 # Class names patterns are ITable*Adaptator and Table*Adaptator.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
88 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
89 # sql::ITablePathAdaptator, sql::TablePathAdaptator /
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
90 # sql::ITableSetAdaptator, sql::TableSetAdaptator /
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
91 # sql::ITableSeqAdaptator, sql::TableSeqAdaptator /
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
92 # sql::ITableMapAdaptator, sql::TableMapAdaptator /
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
93 # sql::ITableMatchAdaptator, sql::TableMatchAdaptator.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
94 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
95
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
96 import logging
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
97 import sys
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
98
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
99 DEFAULT_LEVEL = 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
100 DEFAULT_FORMAT = "%(asctime)s - %(module)s - %(levelname)s - %(message)s"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
101 DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
102
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
103 ## Use this class to create a instance of logging class.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
104 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
105 class LoggerFactory(object):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
106
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
107 def createLogger(name, verbosity = DEFAULT_LEVEL, format = DEFAULT_FORMAT, out = sys.stdout):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
108 log = logging.getLogger(name)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
109
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
110 hasStreamHandler = False
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
111 for handler in log.handlers:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
112 if handler.__class__ == logging.StreamHandler:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
113 hasStreamHandler = True
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
114 break
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
115 if not hasStreamHandler:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
116 formatter = logging.Formatter(format, DATE_FORMAT)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
117 handler = logging.StreamHandler(out)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
118 handler.setFormatter(formatter)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
119 log.addHandler(handler)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
120
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
121 LoggerFactory.setLevel(log, verbosity)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
122 return log
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
123
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
124 createLogger = staticmethod(createLogger)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
125
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
126 def setLevel(log, verbosity):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
127 log.disabled = False
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
128 if verbosity >= 4:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
129 log.setLevel(logging.DEBUG)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
130 elif verbosity == 3:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
131 log.setLevel(logging.INFO)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
132 elif verbosity == 2:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
133 log.setLevel(logging.WARNING)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
134 elif verbosity == 1:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
135 log.setLevel(logging.ERROR)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
136 elif verbosity == 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
137 log.disabled = True
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
138
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
139 setLevel = staticmethod(setLevel)