Skip to content

Commit

Permalink
convert argument fmt into dict if argument is a string
Browse files Browse the repository at this point in the history
FluentRecordFormatter is expecting fmt to be either a callable or a dict. But there is no strict type checking is done to check whether the variable is dict or not.

There are many situations in which `fmt` can have a string value.
For eg: if logger is loading configuration from a config file using [ConfigParser](https://docs.python.org/2/library/configparser.html) , then there is no way to specify a dict inside the config file.

This change is backward compatible from Python2.7 to Python3.8 since `ast.literal_eval` will work in same way since python 2.6+

Signed-off-by: Harish Karumuthil <harish2704@gmail.com>
  • Loading branch information
harish2704 committed Oct 28, 2020
1 parent d1b81ba commit f38ee21
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions fluent/handler.py
Expand Up @@ -79,6 +79,9 @@ def __init__(self, fmt=None, datefmt=None, style='%', fill_missing_fmt_key=False
self._formatter = fmt
self.usesTime = fmt.usesTime
else:
if type(fmt) == str:
import ast
fmt = ast.literal_eval(fmt)
self._fmt_dict = fmt
self._formatter = self._format_by_dict
self.usesTime = self._format_by_dict_uses_time
Expand Down

0 comments on commit f38ee21

Please sign in to comment.