A handler class which writes logging records, appropriately formatted, to a stream. Note that this class does not close the stream, as sys.stdout or sys.stderr may be used.
| 1112 | return '<%s (%s)>' % (self.__class__.__name__, level) |
| 1113 | |
| 1114 | class StreamHandler(Handler): |
| 1115 | """ |
| 1116 | A handler class which writes logging records, appropriately formatted, |
| 1117 | to a stream. Note that this class does not close the stream, as |
| 1118 | sys.stdout or sys.stderr may be used. |
| 1119 | """ |
| 1120 | |
| 1121 | terminator = '\n' |
| 1122 | |
| 1123 | def __init__(self, stream=None): |
| 1124 | """ |
| 1125 | Initialize the handler. |
| 1126 | |
| 1127 | If stream is not specified, sys.stderr is used. |
| 1128 | """ |
| 1129 | Handler.__init__(self) |
| 1130 | if stream is None: |
| 1131 | stream = sys.stderr |
| 1132 | self.stream = stream |
| 1133 | |
| 1134 | def flush(self): |
| 1135 | """ |
| 1136 | Flushes the stream. |
| 1137 | """ |
| 1138 | with self.lock: |
| 1139 | if self.stream and hasattr(self.stream, "flush"): |
| 1140 | self.stream.flush() |
| 1141 | |
| 1142 | def emit(self, record): |
| 1143 | """ |
| 1144 | Emit a record. |
| 1145 | |
| 1146 | If a formatter is specified, it is used to format the record. |
| 1147 | The record is then written to the stream with a trailing newline. If |
| 1148 | exception information is present, it is formatted using |
| 1149 | traceback.print_exception and appended to the stream. If the stream |
| 1150 | has an 'encoding' attribute, it is used to determine how to do the |
| 1151 | output to the stream. |
| 1152 | """ |
| 1153 | try: |
| 1154 | msg = self.format(record) |
| 1155 | stream = self.stream |
| 1156 | # issue 35046: merged two stream.writes into one. |
| 1157 | stream.write(msg + self.terminator) |
| 1158 | self.flush() |
| 1159 | except RecursionError: # See issue 36272 |
| 1160 | raise |
| 1161 | except Exception: |
| 1162 | self.handleError(record) |
| 1163 | |
| 1164 | def setStream(self, stream): |
| 1165 | """ |
| 1166 | Sets the StreamHandler's stream to the specified value, |
| 1167 | if it is different. |
| 1168 | |
| 1169 | Returns the old stream, if the stream was changed, or None |
| 1170 | if it wasn't. |
| 1171 | """ |
no outgoing calls
no test coverage detected
searching dependent graphs…