23
23
To use, simply 'import logging' and log away!
24
24
"""
25
25
26
- import sys , os , time , io , re , traceback , warnings , weakref , collections .abc
26
+ import sys , os , time , io , traceback , warnings , weakref , collections .abc
27
27
28
28
from string import Template
29
- from string import Formatter as StrFormatter
30
-
31
29
32
30
__all__ = ['BASIC_FORMAT' , 'BufferingFormatter' , 'CRITICAL' , 'DEBUG' , 'ERROR' ,
33
31
'FATAL' , 'FileHandler' , 'Filter' , 'Formatter' , 'Handler' , 'INFO' ,
@@ -415,71 +413,33 @@ def makeLogRecord(dict):
415
413
rv .__dict__ .update (dict )
416
414
return rv
417
415
418
-
419
416
#---------------------------------------------------------------------------
420
417
# Formatter classes and functions
421
418
#---------------------------------------------------------------------------
422
- _str_formatter = StrFormatter ()
423
- del StrFormatter
424
-
425
419
426
420
class PercentStyle (object ):
427
421
428
422
default_format = '%(message)s'
429
423
asctime_format = '%(asctime)s'
430
424
asctime_search = '%(asctime)'
431
- validation_pattern = re .compile (r'%\(\w+\)[#0+ -]*(\*|\d+)?(\.(\*|\d+))?[diouxefgcrsa%]' , re .I )
432
425
433
426
def __init__ (self , fmt ):
434
427
self ._fmt = fmt or self .default_format
435
428
436
429
def usesTime (self ):
437
430
return self ._fmt .find (self .asctime_search ) >= 0
438
431
439
- def validate (self ):
440
- """Validate the input format, ensure it matches the correct style"""
441
- if not self .validation_pattern .search (self ._fmt ):
442
- raise ValueError ("Invalid format '%s' for '%s' style" % (self ._fmt , self .default_format [0 ]))
443
-
444
- def _format (self , record ):
445
- return self ._fmt % record .__dict__
446
-
447
432
def format (self , record ):
448
- try :
449
- return self ._format (record )
450
- except KeyError as e :
451
- raise ValueError ('Formatting field not found in record: %s' % e )
452
-
433
+ return self ._fmt % record .__dict__
453
434
454
435
class StrFormatStyle (PercentStyle ):
455
436
default_format = '{message}'
456
437
asctime_format = '{asctime}'
457
438
asctime_search = '{asctime'
458
439
459
- fmt_spec = re .compile (r'^(.?[<>=^])?[+ -]?#?0?(\d+|{\w+})?[,_]?(\.(\d+|{\w+}))?[bcdefgnosx%]?$' , re .I )
460
- field_spec = re .compile (r'^(\d+|\w+)(\.\w+|\[[^]]+\])*$' )
461
-
462
- def _format (self , record ):
440
+ def format (self , record ):
463
441
return self ._fmt .format (** record .__dict__ )
464
442
465
- def validate (self ):
466
- """Validate the input format, ensure it is the correct string formatting style"""
467
- fields = set ()
468
- try :
469
- for _ , fieldname , spec , conversion in _str_formatter .parse (self ._fmt ):
470
- if fieldname :
471
- if not self .field_spec .match (fieldname ):
472
- raise ValueError ('invalid field name/expression: %r' % fieldname )
473
- fields .add (fieldname )
474
- if conversion and conversion not in 'rsa' :
475
- raise ValueError ('invalid conversion: %r' % conversion )
476
- if spec and not self .fmt_spec .match (spec ):
477
- raise ValueError ('bad specifier: %r' % spec )
478
- except ValueError as e :
479
- raise ValueError ('invalid format: %s' % e )
480
- if not fields :
481
- raise ValueError ('invalid format: no fields' )
482
-
483
443
484
444
class StringTemplateStyle (PercentStyle ):
485
445
default_format = '${message}'
@@ -494,24 +454,9 @@ def usesTime(self):
494
454
fmt = self ._fmt
495
455
return fmt .find ('$asctime' ) >= 0 or fmt .find (self .asctime_format ) >= 0
496
456
497
- def validate (self ):
498
- pattern = Template .pattern
499
- fields = set ()
500
- for m in pattern .finditer (self ._fmt ):
501
- d = m .groupdict ()
502
- if d ['named' ]:
503
- fields .add (d ['named' ])
504
- elif d ['braced' ]:
505
- fields .add (d ['braced' ])
506
- elif m .group (0 ) == '$' :
507
- raise ValueError ('invalid format: bare \' $\' not allowed' )
508
- if not fields :
509
- raise ValueError ('invalid format: no fields' )
510
-
511
- def _format (self , record ):
457
+ def format (self , record ):
512
458
return self ._tpl .substitute (** record .__dict__ )
513
459
514
-
515
460
BASIC_FORMAT = "%(levelname)s:%(name)s:%(message)s"
516
461
517
462
_STYLES = {
@@ -565,7 +510,7 @@ class Formatter(object):
565
510
566
511
converter = time .localtime
567
512
568
- def __init__ (self , fmt = None , datefmt = None , style = '%' , validate = True ):
513
+ def __init__ (self , fmt = None , datefmt = None , style = '%' ):
569
514
"""
570
515
Initialize the formatter with specified format strings.
571
516
@@ -585,9 +530,6 @@ def __init__(self, fmt=None, datefmt=None, style='%', validate=True):
585
530
raise ValueError ('Style must be one of: %s' % ',' .join (
586
531
_STYLES .keys ()))
587
532
self ._style = _STYLES [style ][0 ](fmt )
588
- if validate :
589
- self ._style .validate ()
590
-
591
533
self ._fmt = self ._style ._fmt
592
534
self .datefmt = datefmt
593
535
0 commit comments