Revision ee89df69 snf-tools/synnefo_tools/burnin/logger.py

b/snf-tools/synnefo_tools/burnin/logger.py
212 212
    # ----------------------------------
213 213
    # Too many arguments. pylint: disable-msg=R0913
214 214
    def __init__(self, output_dir, verbose=1, use_colors=True,
215
                 in_parallel=False, quiet=False, curr_time=None):
215
                 in_parallel=False, log_level=0, curr_time=None):
216 216
        """Initialize our loggers
217 217

  
218 218
        The file to be used by our file logger will be created inside
......
229 229
        @param use_colors: use colors for out stdout/stderr logger
230 230
        @type in_parallel: boolean
231 231
        @param in_parallel: this signifies that burnin is running in parallel
232
        @type quiet: boolean
233
        @type quiet: do not print logs to stdout/stderr
232
        @type log_level: int
233
        @param log_level: logging level
234
            0: log to console and file
235
            1: log to file only and output the results to console
236
            2: don't log
237
        @type curr_time: datetime.datetime()
238
        @param curr_time: The current time (used as burnin's run id)
234 239

  
235 240
        """
236 241
        self.verbose = verbose
237 242
        self.use_colors = use_colors
238 243
        self.in_parallel = in_parallel
239
        self.quiet = quiet
244
        self.log_level = log_level
240 245

  
241 246
        assert output_dir
242 247

  
......
266 271

  
267 272
    def _create_logging_file(self, timestamp):
268 273
        """Create the logging file"""
274
        if self.log_level > 1:
275
            return
269 276
        self.debug(None, "Using \"%s\" file for logging", self.file_location)
270 277
        with open(self.file_location, 'w') as out_file:
271 278
            out_file.write(SECTION_SEPARATOR + "\n")
......
277 284

  
278 285
    def __del__(self):
279 286
        """Delete the Log object"""
287
        self.print_logfile_to_stdout()
280 288
        # Remove the lock file
281 289
        if hasattr(self, "file_location"):
282 290
            file_lock = os.path.splitext(self.file_location)[0] + LOCK_EXT
......
287 295

  
288 296
    def print_logfile_to_stdout(self):
289 297
        """Print the contents of our log file to stdout"""
290
        with open(self.file_location, 'r') as fin:
291
            sys.stdout.write(fin.read())
298
        if self.log_level == 1:
299
            with open(self.file_location, 'r') as fin:
300
                sys.stdout.write(fin.read())
292 301

  
293 302
    # ----------------------------------
294 303
    # Logging methods
......
358 367

  
359 368
    def _write_to_stdout(self, section, msg):
360 369
        """Write to stdout"""
361
        if not self.quiet:
362
            if section is not None and self.in_parallel:
363
                sys.stdout.write(section + ": " + msg)
364
            else:
365
                sys.stdout.write(msg)
370
        if self.log_level > 0:
371
            return
372
        if section is not None and self.in_parallel:
373
            sys.stdout.write(section + ": " + msg)
374
        else:
375
            sys.stdout.write(msg)
366 376

  
367 377
    def _write_to_stderr(self, section, msg):
368 378
        """Write to stderr"""
369
        if not self.quiet:
370
            if section is not None and self.in_parallel:
371
                sys.stderr.write(section + ": " + msg)
372
            else:
373
                sys.stderr.write(msg)
379
        if self.log_level > 0:
380
            return
381
        if section is not None and self.in_parallel:
382
            sys.stderr.write(section + ": " + msg)
383
        else:
384
            sys.stderr.write(msg)
374 385

  
375 386
    def _write_to_file(self, section, msg):
376 387
        """Write to file"""
388
        if self.log_level > 1:
389
            return
377 390
        _write_log_file(self.file_location, section, msg)
378 391

  
379 392
    # ----------------------------------
......
388 401

  
389 402
        # Add a new section in the logging file
390 403
        test_runned = "  * " + testsuite + "\n"
391
        _write_log_file(self.file_location, SECTION_RUNNED, test_runned)
404
        self._write_to_file(SECTION_RUNNED, test_runned)
392 405

  
393 406
        new_section_entry = \
394 407
            SECTION_SEPARATOR + "\n" + SECTION_PREFIX + testsuite + "\n\n\n\n"
395
        _write_log_file(self.file_location, SECTION_NEW, new_section_entry)
408
        self._write_to_file(SECTION_NEW, new_section_entry)
396 409

  
397 410
        # Add new section to the stdout
398 411
        msg = "Starting testsuite %s" % testsuite
......
408 421
        assert testsuite, "Testsuite name can not be emtpy"
409 422

  
410 423
        # Add our testsuite to Results
411
        _write_log_file(self.file_location, SECTION_PASSED, testsuite)
424
        self._write_to_file(SECTION_PASSED, testsuite)
412 425

  
413 426
        # Add success to stdout
414 427
        msg = "Testsuite %s passed" % testsuite
......
424 437
        assert testsuite, "Testsuite name can not be emtpy"
425 438

  
426 439
        # Add our testsuite to Results
427
        _write_log_file(self.file_location, SECTION_FAILED, testsuite)
440
        self._write_to_file(SECTION_FAILED, testsuite)
428 441

  
429 442
        # Add success to stdout
430 443
        msg = "Testsuite %s failed" % testsuite

Also available in: Unified diff