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

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

#!/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: 

UsePythonLogging = False 

"""Forward Python `lsst.log` messages to Python `logging` package.""" 

 

@classmethod 

def usePythonLogging(cls): 

"""Forward log messages to Python `logging` 

 

Notes 

----- 

This is useful for unit testing when you want to ensure 

that log messages are captured by the testing environment 

as distinct from standard output. 

 

This state only affects messages sent to the `lsst.log` 

package from Python. 

""" 

cls.UsePythonLogging = True 

 

@classmethod 

def doNotUsePythonLogging(cls): 

"""Forward log messages to LSST logging system. 

 

Notes 

----- 

This is the default state. 

""" 

cls.UsePythonLogging = False 

 

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 warning(self, fmt, *args): 

self.warn(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 

if self.UsePythonLogging: 

pylog = logging.getLogger(self.getName()) 

record = logging.LogRecord(self.getName(), LevelTranslator.lsstLog2logging(level), 

filename, frame.f_lineno, msg, None, False, func=funcname) 

pylog.handle(record) 

else: 

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 warning(fmt, *args): 

warn(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 

 

 

def usePythonLogging(): 

Log.usePythonLogging() 

 

 

def doNotUsePythonLogging(): 

Log.doNotUsePythonLogging() 

 

 

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 UsePythonLogging: 

"""Context manager to enable Python log forwarding temporarily.""" 

 

def __init__(self): 

self.current = Log.UsePythonLogging 

 

def __enter__(self): 

Log.usePythonLogging() 

 

def __exit__(self, exc_type, exc_value, traceback): 

Log.UsePythonLogging = self.current 

 

 

class LevelTranslator: 

"""Helper class to translate levels between ``lsst.log`` and Python 

`logging`. 

""" 

@staticmethod 

def lsstLog2logging(level): 

"""Translates from lsst.log/log4cxx levels to `logging` module levels. 

 

Parameters 

---------- 

level : `int` 

Logging level number used by `lsst.log`, typically one of the 

constants defined in this module (`DEBUG`, `INFO`, etc.) 

 

Returns 

------- 

level : `int` 

Correspoding logging level number for Python `logging` module. 

""" 

# Python logging levels are same as lsst.log divided by 1000, 

# logging does not have TRACE level by default but it is OK to use 

# that numeric level and we may even add TRACE later. 

return level//1000 

 

@staticmethod 

def logging2lsstLog(level): 

"""Translates from standard python `logging` module levels to 

lsst.log/log4cxx levels. 

 

Parameters 

---------- 

level : `int` 

Logging level number used by Python `logging`, typically one of 

the constants defined by `logging` module (`logging.DEBUG`, 

`logging.INFO`, etc.) 

 

Returns 

------- 

level : `int` 

Correspoding logging level number for `lsst.log` module. 

""" 

return level*1000 

 

 

class LogHandler(logging.Handler): 

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

 

Notes 

----- 

If this handler is enabled and `lsst.log` has been configured to use 

Python `logging`, the handler will do nothing itself if any other 

handler has been registered with the Python logger. If it does not 

think that anything else is handling the message it will attempt to 

send the message via a default `~logging.StreamHandler`. The safest 

approach is to configure the logger with an additional handler 

(possibly the ROOT logger) if `lsst.log` is to be configured to use 

Python logging. 

""" 

 

def __init__(self, level=logging.NOTSET): 

logging.Handler.__init__(self, level=level) 

# Format as a simple message because lsst.log will format the 

# message a second time. 

self.formatter = logging.Formatter(fmt="%(message)s") 

 

def handle(self, record): 

logger = Log.getLogger(record.name) 

if logger.isEnabledFor(LevelTranslator.logging2lsstLog(record.levelno)): 

logging.Handler.handle(self, record) 

 

def emit(self, record): 

if Log.UsePythonLogging: 

# Do not forward this message to lsst.log since this may cause 

# a logging loop. 

 

# Work out whether any other handler is going to be invoked 

# for this logger. 

pylgr = logging.getLogger(record.name) 

 

# If another handler is registered that is not LogHandler 

# we ignore this request 

if any(not isinstance(h, self.__class__) for h in pylgr.handlers): 

return 

 

# If the parent has handlers and propagation is enabled 

# we punt as well (and if a LogHandler is involved then we will 

# ask the same question when we get to it). 

if pylgr.parent and pylgr.parent.hasHandlers() and pylgr.propagate: 

return 

 

# Force this message to appear somewhere. 

# If something else should happen then the caller should add a 

# second Handler. 

stream = logging.StreamHandler() 

stream.setFormatter(logging.Formatter(fmt="%(name)s %(levelname)s (fallback): %(message)s")) 

stream.handle(record) 

return 

 

logger = Log.getLogger(record.name) 

# Use standard formatting class to format message part of the record 

message = self.formatter.format(record) 

 

logger.logMsg(LevelTranslator.logging2lsstLog(record.levelno), 

record.filename, record.funcName, 

record.lineno, message)