Turns on formatted logging output as configured. This is called automatically by `tornado.options.parse_command_line` and `tornado.options.parse_config_file`.
(
options: Any = None, logger: Optional[logging.Logger] = None
)
| 213 | |
| 214 | |
| 215 | def enable_pretty_logging( |
| 216 | options: Any = None, logger: Optional[logging.Logger] = None |
| 217 | ) -> None: |
| 218 | """Turns on formatted logging output as configured. |
| 219 | |
| 220 | This is called automatically by `tornado.options.parse_command_line` |
| 221 | and `tornado.options.parse_config_file`. |
| 222 | """ |
| 223 | if options is None: |
| 224 | import tornado.options |
| 225 | |
| 226 | options = tornado.options.options |
| 227 | if options.logging is None or options.logging.lower() == "none": |
| 228 | return |
| 229 | if logger is None: |
| 230 | logger = logging.getLogger() |
| 231 | logger.setLevel(getattr(logging, options.logging.upper())) |
| 232 | if options.log_file_prefix: |
| 233 | rotate_mode = options.log_rotate_mode |
| 234 | if rotate_mode == "size": |
| 235 | channel = logging.handlers.RotatingFileHandler( |
| 236 | filename=options.log_file_prefix, |
| 237 | maxBytes=options.log_file_max_size, |
| 238 | backupCount=options.log_file_num_backups, |
| 239 | encoding="utf-8", |
| 240 | ) # type: logging.Handler |
| 241 | elif rotate_mode == "time": |
| 242 | channel = logging.handlers.TimedRotatingFileHandler( |
| 243 | filename=options.log_file_prefix, |
| 244 | when=options.log_rotate_when, |
| 245 | interval=options.log_rotate_interval, |
| 246 | backupCount=options.log_file_num_backups, |
| 247 | encoding="utf-8", |
| 248 | ) |
| 249 | else: |
| 250 | error_message = ( |
| 251 | "The value of log_rotate_mode option should be " |
| 252 | + '"size" or "time", not "%s".' % rotate_mode |
| 253 | ) |
| 254 | raise ValueError(error_message) |
| 255 | channel.setFormatter(LogFormatter(color=False)) |
| 256 | logger.addHandler(channel) |
| 257 | |
| 258 | if options.log_to_stderr or (options.log_to_stderr is None and not logger.handlers): |
| 259 | # Set up color if we are in a tty and curses is installed |
| 260 | channel = logging.StreamHandler() |
| 261 | channel.setFormatter(LogFormatter()) |
| 262 | logger.addHandler(channel) |
| 263 | |
| 264 | |
| 265 | def define_logging_options(options: Any = None) -> None: |