Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

#!/usr/bin/env python 

 

# 

# LSST Data Management System 

# Copyright 2013 LSST Corporation. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

import logging 

import inspect 

import os 

 

from lsst.utils import continueClass 

 

from .log import Log 

 

TRACE = 5000 

DEBUG = 10000 

INFO = 20000 

WARN = 30000 

ERROR = 40000 

FATAL = 50000 

 

 

@continueClass # noqa F811 redefinition 

class Log: 

def trace(self, fmt, *args): 

self._log(Log.TRACE, False, fmt, *args) 

 

def debug(self, fmt, *args): 

self._log(Log.DEBUG, False, fmt, *args) 

 

def info(self, fmt, *args): 

self._log(Log.INFO, False, fmt, *args) 

 

def warn(self, fmt, *args): 

self._log(Log.WARN, False, fmt, *args) 

 

def error(self, fmt, *args): 

self._log(Log.ERROR, False, fmt, *args) 

 

def fatal(self, fmt, *args): 

self._log(Log.FATAL, False, fmt, *args) 

 

def tracef(self, fmt, *args, **kwargs): 

self._log(Log.TRACE, True, fmt, *args, **kwargs) 

 

def debugf(self, fmt, *args, **kwargs): 

self._log(Log.DEBUG, True, fmt, *args, **kwargs) 

 

def infof(self, fmt, *args, **kwargs): 

self._log(Log.INFO, True, fmt, *args, **kwargs) 

 

def warnf(self, fmt, *args, **kwargs): 

self._log(Log.WARN, True, fmt, *args, **kwargs) 

 

def errorf(self, fmt, *args, **kwargs): 

self._log(Log.ERROR, True, fmt, *args, **kwargs) 

 

def fatalf(self, fmt, *args, **kwargs): 

self._log(Log.FATAL, True, fmt, *args, **kwargs) 

 

def _log(self, level, use_format, fmt, *args, **kwargs): 

if self.isEnabledFor(level): 

frame = inspect.currentframe().f_back # calling method 

frame = frame.f_back # original log location 

filename = os.path.split(frame.f_code.co_filename)[1] 

funcname = frame.f_code.co_name 

if use_format: 

msg = fmt.format(*args, **kwargs) if args or kwargs else fmt 

else: 

msg = fmt % args if args else fmt 

self.logMsg(level, filename, funcname, frame.f_lineno, msg) 

 

 

# Export static functions from Log class to module namespace 

 

 

def configure(*args): 

Log.configure(*args) 

 

 

def configure_prop(properties): 

Log.configure_prop(properties) 

 

 

def getDefaultLoggerName(): 

return Log.getDefaultLoggerName() 

 

 

def pushContext(name): 

Log.pushContext(name) 

 

 

def popContext(): 

Log.popContext() 

 

 

def MDC(key, value): 

Log.MDC(key, str(value)) 

 

 

def MDCRemove(key): 

Log.MDCRemove(key) 

 

 

def MDCRegisterInit(func): 

Log.MDCRegisterInit(func) 

 

 

def setLevel(loggername, level): 

Log.getLogger(loggername).setLevel(level) 

 

 

def getLevel(loggername): 

Log.getLogger(loggername).getLevel() 

 

 

def isEnabledFor(logger, level): 

Log.getLogger(logger).isEnabledFor(level) 

 

 

def log(loggername, level, fmt, *args, **kwargs): 

Log.getLogger(loggername)._log(level, False, fmt, *args) 

 

 

def trace(fmt, *args): 

Log.getDefaultLogger()._log(TRACE, False, fmt, *args) 

 

 

def debug(fmt, *args): 

Log.getDefaultLogger()._log(DEBUG, False, fmt, *args) 

 

 

def info(fmt, *args): 

Log.getDefaultLogger()._log(INFO, False, fmt, *args) 

 

 

def warn(fmt, *args): 

Log.getDefaultLogger()._log(WARN, False, fmt, *args) 

 

 

def error(fmt, *args): 

Log.getDefaultLogger()._log(ERROR, False, fmt, *args) 

 

 

def fatal(fmt, *args): 

Log.getDefaultLogger()._log(FATAL, False, fmt, *args) 

 

 

def logf(loggername, level, fmt, *args, **kwargs): 

Log.getLogger(loggername)._log(level, True, fmt, *args, **kwargs) 

 

 

def tracef(fmt, *args, **kwargs): 

Log.getDefaultLogger()._log(TRACE, True, fmt, *args, **kwargs) 

 

 

def debugf(fmt, *args, **kwargs): 

Log.getDefaultLogger()._log(DEBUG, True, fmt, *args, **kwargs) 

 

 

def infof(fmt, *args, **kwargs): 

Log.getDefaultLogger()._log(INFO, True, fmt, *args, **kwargs) 

 

 

def warnf(fmt, *args, **kwargs): 

Log.getDefaultLogger()._log(WARN, True, fmt, *args, **kwargs) 

 

 

def errorf(fmt, *args, **kwargs): 

Log.getDefaultLogger()._log(ERROR, True, fmt, *args, **kwargs) 

 

 

def fatalf(fmt, *args, **kwargs): 

Log.getDefaultLogger()._log(FATAL, True, fmt, *args, **kwargs) 

 

 

def lwpID(): 

return Log.lwpID 

 

 

class LogContext(object): 

"""Context manager for logging.""" 

 

def __init__(self, name=None, level=None): 

self.name = name 

self.level = level 

 

def __enter__(self): 

self.open() 

return self 

 

def __exit__(self, type, value, traceback): 

self.close() 

 

def __del__(self): 

self.close() 

 

def open(self): 

if self.name is not None: 

Log.pushContext(self.name) 

if self.level is not None: 

Log.getDefaultLogger().setLevel(self.level) 

 

def close(self): 

if self.name is not None: 

Log.popContext() 

self.name = None 

 

def setLevel(self, level): 

Log.getDefaultLogger().setLevel(level) 

 

def getLevel(self): 

return Log.getDefaultLogger().getLevel() 

 

def isEnabledFor(self, level): 

return Log.getDefaultLogger().isEnabledFor(level) 

 

 

class LogHandler(logging.Handler): 

"""Handler for Python logging module that emits to LSST logging.""" 

 

def __init__(self, name=None, level=None): 

self.context = LogContext(name=name, level=level) 

self.context.open() 

logging.Handler.__init__(self) 

 

def __del__(self): 

self.close() 

 

def close(self): 

249 ↛ 252line 249 didn't jump to line 252, because the condition on line 249 was never false if self.context is not None: 

self.context.close() 

self.context = None 

logging.Handler.close(self) 

 

def handle(self, record): 

255 ↛ exitline 255 didn't return from function 'handle', because the condition on line 255 was never false if self.context.isEnabledFor(self.translateLevel(record.levelno)): 

logging.Handler.handle(self, record) 

 

def emit(self, record): 

Log.getLogger(record.name).logMsg(self.translateLevel(record.levelno), 

record.filename, record.funcName, 

record.lineno, 

record.msg % record.args) 

 

def translateLevel(self, levelno): 

""" 

Translates from standard python logging module levels 

to standard log4cxx levels. 

""" 

return levelno*1000