Salome HOME
Python 3 compatibility improvement (UTF-8) and data interface changes
[modules/adao.git] / src / daComposant / daCore / ExtendedLogging.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2017 EDF R&D
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
22
23 """
24     Ce module permet de mettre en place un logging utilisable partout dans
25     l'application, par défaut à la console, et si nécessaire dans un fichier.
26
27     Il doit être appelé en premier dans AssimilationStudy (mais pas directement
28     dans les applications utilisateurs), en l'important et en instanciant un
29     objet :
30         import ExtendedLogging ; ExtendedLogging.ExtendedLogging()
31
32     Par défaut, seuls les messages du niveau WARNING ou au-delà sont disponibles
33     (donc les simples messages d'info ne sont pas disponibles), ce que l'on peut
34     changer à l'instanciation avec le mot-clé "level" :
35         import ExtendedLogging ; ExtendedLogging.ExtendedLogging(level=20)
36
37     On peut éventuellement demander à l'objet de sortir aussi les messages dans
38     un fichier (noms par défaut : AssimilationStudy.log, niveau NOTSET) :
39         import ExtendedLogging ; ExtendedLogging.ExtendedLogging().setLogfile()
40
41     Si on veut changer le nom du fichier ou le niveau global de message, il faut
42     récupérer l'instance et appliquer les méthodes :
43         import ExtendedLogging
44         log = ExtendedLogging.ExtendedLogging()
45         import logging
46         log.setLevel(logging.DEBUG)
47         log.setLogfile(filename="toto.log", filemode="a", level=logging.WARNING)
48     et on change éventuellement le niveau avec :
49         log.setLogfileLevel(logging.INFO)
50
51     Ensuite, n'importe où dans les applications, il suffit d'utiliser le module
52     "logging" (avec un petit "l") :
53         import logging
54         log = logging.getLogger(NAME) # Avec rien (recommandé) ou un nom NAME
55         log.critical("...")
56         log.error("...")
57         log.warning("...")
58         log.info("...")
59         log.debug("...")
60     ou encore plus simplement :
61         import logging
62         logging.info("...")
63
64     Dans une application, à n'importe quel endroit et autant de fois qu'on veut,
65     on peut changer le niveau global de message en utilisant par exemple :
66         import logging
67         log = logging.getLogger(NAME) # Avec rien (recommandé) ou un nom NAME
68         log.setLevel(logging.DEBUG)
69
70     On rappelle les niveaux (attributs de "logging") et leur ordre :
71         NOTSET=0 < DEBUG=10 < INFO=20 < WARNING=30 < ERROR=40 < CRITICAL=50
72 """
73 __author__ = "Jean-Philippe ARGAUD"
74 __all__ = []
75
76 import os
77 import sys
78 import logging
79 from daCore import PlatformInfo
80
81 LOGFILE = os.path.join(os.path.abspath(os.curdir),"AssimilationStudy.log")
82
83 # ==============================================================================
84 class ExtendedLogging(object):
85     """
86     Logger général pour disposer conjointement de la sortie standard et de la
87     sortie sur fichier
88     """
89     def __init__(self, level=logging.WARNING):
90         """
91         Initialise un logging à la console pour TOUS les niveaux de messages.
92         """
93         logging.basicConfig(
94             format = '%(levelname)-8s %(message)s',
95             level  = level,
96             stream = sys.stdout,
97             )
98         self.__logfile = None
99         #
100         # Initialise l'affichage de logging
101         # ---------------------------------
102         p = PlatformInfo.PlatformInfo()
103         #
104         logging.info( "--------------------------------------------------" )
105         logging.info( p.getName()+" version "+p.getVersion() )
106         logging.info( "--------------------------------------------------" )
107         logging.info( "Library availability:" )
108         logging.info( "- Python.......: True" )
109         logging.info( "- Numpy........: True" )
110         logging.info( "- Scipy........: "+str(PlatformInfo.has_scipy) )
111         logging.info( "- Matplotlib...: "+str(PlatformInfo.has_matplotlib) )
112         logging.info( "- Gnuplot......: "+str(PlatformInfo.has_scipy) )
113         logging.info( "- Sphinx.......: "+str(PlatformInfo.has_sphinx) )
114         logging.info( "- Nlopt........: "+str(PlatformInfo.has_nlopt) )
115         logging.info( "Library versions:" )
116         logging.info( "- Python.......:"+p.getPythonVersion() )
117         logging.info( "- Numpy........:"+p.getNumpyVersion() )
118         logging.info( "- Scipy........:"+p.getScipyVersion() )
119         logging.info( "" )
120
121     def setLogfile(self, filename=LOGFILE, filemode="w", level=logging.NOTSET):
122         """
123         Permet de disposer des messages dans un fichier EN PLUS de la console.
124         """
125         if self.__logfile is not None:
126             # Supprime le précédent mode de stockage fichier s'il exsitait
127             logging.getLogger().removeHandler(self.__logfile)
128         self.__logfile = logging.FileHandler(filename, filemode)
129         self.__logfile.setLevel(level)
130         self.__logfile.setFormatter(
131             logging.Formatter('%(asctime)s %(levelname)-8s %(message)s',
132                               '%d %b %Y %H:%M:%S'))
133         logging.getLogger().addHandler(self.__logfile)
134
135     def setLogfileLevel(self, level=logging.NOTSET ):
136         """
137         Permet de changer le niveau des messages stockés en fichier. Il ne sera
138         pris en compte que s'il est supérieur au niveau global.
139         """
140         self.__logfile.setLevel(level)
141
142     def getLevel(self):
143         """
144         Renvoie le niveau de logging sous forme texte
145         """
146         return logging.getLevelName( logging.getLogger().getEffectiveLevel() )
147
148 # ==============================================================================
149 if __name__ == "__main__":
150     print('\n AUTODIAGNOSTIC \n')