@@ -163,15 +163,8 @@ class Logger:
163
163
164
164
_lock = threading .Lock ()
165
165
166
- def __init__ (self , extra , patcher , exception , record , lazy , ansi , raw , depth ):
167
- self ._extra = extra
168
- self ._patcher = patcher
169
- self ._record = record
170
- self ._exception = exception
171
- self ._lazy = lazy
172
- self ._ansi = ansi
173
- self ._raw = raw
174
- self ._depth = depth
166
+ def __init__ (self , exception , depth , record , lazy , ansi , raw , patcher , extra ):
167
+ self ._options = (exception , depth , record , lazy , ansi , raw , patcher , extra )
175
168
176
169
def add (
177
170
self ,
@@ -983,16 +976,15 @@ def __exit__(self_, type_, value, traceback_):
983
976
if not issubclass (type_ , exception ):
984
977
return False
985
978
986
- if self_ ._from_decorator :
987
- from_decorator = True
988
- else :
989
- from_decorator = False
979
+ from_decorator = self_ ._from_decorator
980
+ _ , depth , _ , * options = self ._options
981
+
982
+ if from_decorator :
983
+ depth += 1
990
984
991
- exception_ = (type_ , value , traceback_ )
985
+ catch_options = [ (type_ , value , traceback_ ), depth , True ] + options
992
986
level_id , static_level_no = self ._dynamic_level (level )
993
- self ._log (
994
- level_id , static_level_no , from_decorator , exception_ , True , message , (), {}
995
- )
987
+ self ._log (level_id , static_level_no , from_decorator , catch_options , message , (), {})
996
988
997
989
return not reraise
998
990
@@ -1088,7 +1080,7 @@ def opt(self, *, exception=None, record=False, lazy=False, ansi=False, raw=False
1088
1080
>>> func()
1089
1081
[18:11:54] DEBUG in 'func' - Get parent context
1090
1082
"""
1091
- return Logger (self . _extra , self . _patcher , exception , record , lazy , ansi , raw , depth )
1083
+ return Logger (exception , depth , record , lazy , ansi , raw , * self . _options [ - 2 :] )
1092
1084
1093
1085
def bind (_self , ** kwargs ):
1094
1086
"""Bind attributes to the ``extra`` dict of each logged message record.
@@ -1124,16 +1116,8 @@ def bind(_self, **kwargs):
1124
1116
>>> instance_2.call("Second instance")
1125
1117
127.0.0.1 - Second instance
1126
1118
"""
1127
- return Logger (
1128
- {** _self ._extra , ** kwargs },
1129
- _self ._patcher ,
1130
- _self ._exception ,
1131
- _self ._record ,
1132
- _self ._lazy ,
1133
- _self ._ansi ,
1134
- _self ._raw ,
1135
- _self ._depth ,
1136
- )
1119
+ * options , extra = _self ._options
1120
+ return Logger (* options , {** extra , ** kwargs })
1137
1121
1138
1122
def patch (self , patcher ):
1139
1123
"""Attach a function to modify the record dict created by each logging call.
@@ -1178,16 +1162,8 @@ def patch(self, patcher):
1178
1162
... level, message = record["level"], record["message"]
1179
1163
... logger.patch(lambda r: r.update(record)).log(level, message)
1180
1164
"""
1181
- return Logger (
1182
- self ._extra ,
1183
- patcher ,
1184
- self ._exception ,
1185
- self ._record ,
1186
- self ._lazy ,
1187
- self ._ansi ,
1188
- self ._raw ,
1189
- self ._depth ,
1190
- )
1165
+ * options , _ , extra = self ._options
1166
+ return Logger (* options , patcher , extra )
1191
1167
1192
1168
def level (self , name , no = None , color = None , icon = None ):
1193
1169
"""Add, update or retrieve a logging level.
@@ -1566,16 +1542,14 @@ def _find_iter(fileobj, regex, chunk):
1566
1542
buffer = buffer [end :]
1567
1543
yield from matches [:- 1 ]
1568
1544
1569
- def _log (
1570
- self , level_id , static_level_no , from_decorator , exception , record_ , message , args , kwargs
1571
- ):
1545
+ @staticmethod
1546
+ def _log (level_id , static_level_no , from_decorator , options , message , args , kwargs ):
1572
1547
if not Logger ._handlers :
1573
1548
return
1574
1549
1575
- if from_decorator :
1576
- frame = get_frame (self ._depth + 3 )
1577
- else :
1578
- frame = get_frame (self ._depth + 2 )
1550
+ (exception , depth , record , lazy , ansi , raw , patcher , extra ) = options
1551
+
1552
+ frame = get_frame (depth + 2 )
1579
1553
1580
1554
try :
1581
1555
name = frame .f_globals ["__name__" ]
@@ -1654,10 +1628,10 @@ def _log(
1654
1628
else :
1655
1629
exception = None
1656
1630
1657
- record = {
1631
+ log_record = {
1658
1632
"elapsed" : elapsed ,
1659
1633
"exception" : exception ,
1660
- "extra" : {** Logger ._extra_class , ** self . _extra },
1634
+ "extra" : {** Logger ._extra_class , ** extra },
1661
1635
"file" : file_recattr ,
1662
1636
"function" : code .co_name ,
1663
1637
"level" : level_recattr ,
@@ -1670,69 +1644,61 @@ def _log(
1670
1644
"time" : current_datetime ,
1671
1645
}
1672
1646
1673
- if self . _lazy :
1647
+ if lazy :
1674
1648
args = [arg () for arg in args ]
1675
1649
kwargs = {key : value () for key , value in kwargs .items ()}
1676
1650
1677
- if record_ :
1678
- record ["message" ] = message .format (* args , ** kwargs , record = record )
1651
+ if record :
1652
+ log_record ["message" ] = message .format (* args , ** kwargs , record = log_record )
1679
1653
elif args or kwargs :
1680
- record ["message" ] = message .format (* args , ** kwargs )
1654
+ log_record ["message" ] = message .format (* args , ** kwargs )
1681
1655
1682
1656
if Logger ._patcher_class :
1683
- Logger ._patcher_class (record )
1657
+ Logger ._patcher_class (log_record )
1684
1658
1685
- if self . _patcher :
1686
- self . _patcher ( record )
1659
+ if patcher :
1660
+ patcher ( log_record )
1687
1661
1688
1662
for handler in Logger ._handlers .values ():
1689
- handler .emit (record , level_id , from_decorator , self . _ansi , self . _raw )
1663
+ handler .emit (log_record , level_id , from_decorator , ansi , raw )
1690
1664
1691
1665
def trace (_self , _message , * args , ** kwargs ):
1692
1666
r"""Log ``_message.format(*args, **kwargs)`` with severity ``'TRACE'``."""
1693
- _self ._log ("TRACE" , None , False , _self ._exception , _self . _record , _message , args , kwargs )
1667
+ _self ._log ("TRACE" , None , False , _self ._options , _message , args , kwargs )
1694
1668
1695
1669
def debug (_self , _message , * args , ** kwargs ):
1696
1670
r"""Log ``_message.format(*args, **kwargs)`` with severity ``'DEBUG'``."""
1697
- _self ._log ("DEBUG" , None , False , _self ._exception , _self . _record , _message , args , kwargs )
1671
+ _self ._log ("DEBUG" , None , False , _self ._options , _message , args , kwargs )
1698
1672
1699
1673
def info (_self , _message , * args , ** kwargs ):
1700
1674
r"""Log ``_message.format(*args, **kwargs)`` with severity ``'INFO'``."""
1701
- _self ._log ("INFO" , None , False , _self ._exception , _self . _record , _message , args , kwargs )
1675
+ _self ._log ("INFO" , None , False , _self ._options , _message , args , kwargs )
1702
1676
1703
1677
def success (_self , _message , * args , ** kwargs ):
1704
1678
r"""Log ``_message.format(*args, **kwargs)`` with severity ``'SUCCESS'``."""
1705
- _self ._log ("SUCCESS" , None , False , _self ._exception , _self . _record , _message , args , kwargs )
1679
+ _self ._log ("SUCCESS" , None , False , _self ._options , _message , args , kwargs )
1706
1680
1707
1681
def warning (_self , _message , * args , ** kwargs ):
1708
1682
r"""Log ``_message.format(*args, **kwargs)`` with severity ``'WARNING'``."""
1709
- _self ._log ("WARNING" , None , False , _self ._exception , _self . _record , _message , args , kwargs )
1683
+ _self ._log ("WARNING" , None , False , _self ._options , _message , args , kwargs )
1710
1684
1711
1685
def error (_self , _message , * args , ** kwargs ):
1712
1686
r"""Log ``_message.format(*args, **kwargs)`` with severity ``'ERROR'``."""
1713
- _self ._log ("ERROR" , None , False , _self ._exception , _self . _record , _message , args , kwargs )
1687
+ _self ._log ("ERROR" , None , False , _self ._options , _message , args , kwargs )
1714
1688
1715
1689
def critical (_self , _message , * args , ** kwargs ):
1716
1690
r"""Log ``_message.format(*args, **kwargs)`` with severity ``'CRITICAL'``."""
1717
- _self ._log ("CRITICAL" , None , False , _self ._exception , _self . _record , _message , args , kwargs )
1691
+ _self ._log ("CRITICAL" , None , False , _self ._options , _message , args , kwargs )
1718
1692
1719
1693
def exception (_self , _message , * args , ** kwargs ):
1720
1694
r"""Convenience method for logging an ``'ERROR'`` with exception information."""
1721
- _self ._log ("ERROR" , None , False , True , _self ._record , _message , args , kwargs )
1695
+ options = (True ,) + _self ._options [1 :]
1696
+ _self ._log ("ERROR" , None , False , options , _message , args , kwargs )
1722
1697
1723
1698
def log (_self , _level , _message , * args , ** kwargs ):
1724
1699
r"""Log ``_message.format(*args, **kwargs)`` with severity ``_level``."""
1725
1700
level_id , static_level_no = _self ._dynamic_level (_level )
1726
- _self ._log (
1727
- level_id ,
1728
- static_level_no ,
1729
- False ,
1730
- _self ._exception ,
1731
- _self ._record ,
1732
- _message ,
1733
- args ,
1734
- kwargs ,
1735
- )
1701
+ _self ._log (level_id , static_level_no , False , _self ._options , _message , args , kwargs )
1736
1702
1737
1703
@staticmethod
1738
1704
@functools .lru_cache (maxsize = 32 )
0 commit comments