|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: gbk -*- |
| 3 | + |
| 4 | +""" |
| 5 | +File: jsonformatter.py |
| 6 | +Author: Me |
| 7 | + |
| 8 | +Github: https://github.com/yourname |
| 9 | +Description: jsonformatter.py |
| 10 | +""" |
| 11 | +import datetime |
| 12 | +import logging |
| 13 | +import os |
| 14 | +import random |
| 15 | +import unittest |
| 16 | +from collections import OrderedDict |
| 17 | +from logging.config import fileConfig |
| 18 | + |
| 19 | + |
| 20 | +if __file__ == 'test_windows.py': |
| 21 | + import sys |
| 22 | + sys.path.insert(0, '..') |
| 23 | + |
| 24 | +from jsonformatter import JsonFormatter |
| 25 | + |
| 26 | + |
| 27 | +class JsonFormatterTest(unittest.TestCase): |
| 28 | + |
| 29 | + def test_default_config(self): |
| 30 | + root = logging.getLogger() |
| 31 | + root.setLevel(logging.INFO) |
| 32 | + |
| 33 | + datefmt = None |
| 34 | + sh = logging.StreamHandler() |
| 35 | + formatter = JsonFormatter() |
| 36 | + sh.setFormatter(formatter) |
| 37 | + |
| 38 | + sh.setLevel(logging.INFO) |
| 39 | + |
| 40 | + root.addHandler(sh) |
| 41 | + |
| 42 | + root.info("test %s config", 'default') |
| 43 | + |
| 44 | + def test_string_format(self): |
| 45 | + STRING_FORMAT = '''{ |
| 46 | + "Name": "name", |
| 47 | + "Levelno": "levelno", |
| 48 | + "Levelname": "levelname", |
| 49 | + "Pathname": "pathname", |
| 50 | + "Filename": "filename", |
| 51 | + "Module": "module", |
| 52 | + "Lineno": "lineno", |
| 53 | + "FuncName": "funcName", |
| 54 | + "Created": "created", |
| 55 | + "Asctime": "asctime", |
| 56 | + "Msecs": "msecs", |
| 57 | + "RelativeCreated": "relativeCreated", |
| 58 | + "Thread": "thread", |
| 59 | + "ThreadName": "threadName", |
| 60 | + "Process": "process", |
| 61 | + "Message": "message" |
| 62 | + }''' |
| 63 | + root = logging.getLogger() |
| 64 | + root.setLevel(logging.INFO) |
| 65 | + |
| 66 | + datefmt = None |
| 67 | + sh = logging.StreamHandler() |
| 68 | + formatter = JsonFormatter(STRING_FORMAT, datefmt) |
| 69 | + sh.setFormatter(formatter) |
| 70 | + |
| 71 | + sh.setLevel(logging.INFO) |
| 72 | + |
| 73 | + root.addHandler(sh) |
| 74 | + |
| 75 | + root.info("test %s format", 'string') |
| 76 | + |
| 77 | + def test_format_style(self): |
| 78 | + FORMT_STYLE = { |
| 79 | + "name": "name", |
| 80 | + "levelno": "levelno", |
| 81 | + "levelname": "levelname", |
| 82 | + "pathname": "pathname", |
| 83 | + "filename": "filename", |
| 84 | + "module": "module", |
| 85 | + "lineno": "lineno", |
| 86 | + "funcName": "funcName", |
| 87 | + "created": "created", |
| 88 | + "asctime": "asctime", |
| 89 | + "msecs": "msecs", |
| 90 | + "relativeCreated": "relativeCreated", |
| 91 | + "thread": "thread", |
| 92 | + "threadName": "threadName", |
| 93 | + "process": "process", |
| 94 | + "message": "{message}" |
| 95 | + } |
| 96 | + |
| 97 | + root = logging.getLogger() |
| 98 | + root.setLevel(logging.INFO) |
| 99 | + |
| 100 | + datefmt = None |
| 101 | + sh = logging.StreamHandler() |
| 102 | + formatter = JsonFormatter(FORMT_STYLE, datefmt, '{') |
| 103 | + sh.setFormatter(formatter) |
| 104 | + |
| 105 | + sh.setLevel(logging.INFO) |
| 106 | + |
| 107 | + root.addHandler(sh) |
| 108 | + |
| 109 | + root.info("test %s style", 'format') |
| 110 | + |
| 111 | + def test_template_style(self): |
| 112 | + TEMPLATE_STYLE = { |
| 113 | + "name": "name", |
| 114 | + "levelno": "levelno", |
| 115 | + "levelname": "levelname", |
| 116 | + "pathname": "pathname", |
| 117 | + "filename": "filename", |
| 118 | + "module": "module", |
| 119 | + "lineno": "lineno", |
| 120 | + "funcName": "funcName", |
| 121 | + "created": "created", |
| 122 | + "asctime": "asctime", |
| 123 | + "msecs": "msecs", |
| 124 | + "relativeCreated": "relativeCreated", |
| 125 | + "thread": "thread", |
| 126 | + "threadName": "threadName", |
| 127 | + "process": "process", |
| 128 | + "message": "${message}" |
| 129 | + } |
| 130 | + root = logging.getLogger() |
| 131 | + root.setLevel(logging.INFO) |
| 132 | + |
| 133 | + datefmt = None |
| 134 | + sh = logging.StreamHandler() |
| 135 | + formatter = JsonFormatter(TEMPLATE_STYLE, datefmt, '$') |
| 136 | + sh.setFormatter(formatter) |
| 137 | + |
| 138 | + sh.setLevel(logging.INFO) |
| 139 | + |
| 140 | + root.addHandler(sh) |
| 141 | + |
| 142 | + root.info("test %s style", 'template') |
| 143 | + |
| 144 | + def test_percent_style_unicode(self): |
| 145 | + root = logging.getLogger() |
| 146 | + root.setLevel(logging.INFO) |
| 147 | + |
| 148 | + formatter = JsonFormatter("""{"log":"%(message)s"}""", style="%", encoding='gbk', ensure_ascii=False) |
| 149 | + |
| 150 | + sh = logging.StreamHandler() |
| 151 | + sh.setFormatter(formatter) |
| 152 | + sh.setLevel(logging.INFO) |
| 153 | + |
| 154 | + root.addHandler(sh) |
| 155 | + root.info('test percent style unicode: %s', 'ÖÐÎÄ') |
| 156 | + |
| 157 | + def test_format_style_unicode(self): |
| 158 | + root = logging.getLogger() |
| 159 | + root.setLevel(logging.INFO) |
| 160 | + |
| 161 | + formatter = JsonFormatter("""{"log":"{message}"}""", style="{", encoding='gbk', ensure_ascii=False) |
| 162 | + |
| 163 | + sh = logging.StreamHandler() |
| 164 | + sh.setFormatter(formatter) |
| 165 | + sh.setLevel(logging.INFO) |
| 166 | + |
| 167 | + root.addHandler(sh) |
| 168 | + root.info('test format style unicode: %s', 'ÖÐÎÄ') |
| 169 | + |
| 170 | + def test_template_style_unicode(self): |
| 171 | + root = logging.getLogger() |
| 172 | + root.setLevel(logging.INFO) |
| 173 | + |
| 174 | + formatter = JsonFormatter("""{"log":"${message}"}""", style="$", encoding='gbk', ensure_ascii=False) |
| 175 | + |
| 176 | + sh = logging.StreamHandler() |
| 177 | + sh.setFormatter(formatter) |
| 178 | + sh.setLevel(logging.INFO) |
| 179 | + |
| 180 | + root.addHandler(sh) |
| 181 | + root.info('test template style unicode: %s', 'ÖÐÎÄ') |
| 182 | + |
| 183 | + def test_dict_format(self): |
| 184 | + DICT_FORMAT = { |
| 185 | + "name": "name", |
| 186 | + "levelno": "levelno", |
| 187 | + "levelname": "levelname", |
| 188 | + "pathname": "pathname", |
| 189 | + "filename": "filename", |
| 190 | + "module": "module", |
| 191 | + "lineno": "lineno", |
| 192 | + "funcName": "funcName", |
| 193 | + "created": "created", |
| 194 | + "asctime": "asctime", |
| 195 | + "msecs": "msecs", |
| 196 | + "relativeCreated": "relativeCreated", |
| 197 | + "thread": "thread", |
| 198 | + "threadName": "threadName", |
| 199 | + "process": "process", |
| 200 | + "message": "message" |
| 201 | + } |
| 202 | + root = logging.getLogger() |
| 203 | + root.setLevel(logging.INFO) |
| 204 | + |
| 205 | + datefmt = None |
| 206 | + sh = logging.StreamHandler() |
| 207 | + formatter = JsonFormatter(DICT_FORMAT, datefmt) |
| 208 | + sh.setFormatter(formatter) |
| 209 | + |
| 210 | + sh.setLevel(logging.INFO) |
| 211 | + |
| 212 | + root.addHandler(sh) |
| 213 | + |
| 214 | + root.info("test %s format", 'dict') |
| 215 | + |
| 216 | + def test_ordered_dict_format(self): |
| 217 | + ORDERED_DICT_FORMAT = OrderedDict([ |
| 218 | + ("name", "name"), |
| 219 | + ("levelno", "levelno"), |
| 220 | + ("levelname", "levelname"), |
| 221 | + ("pathname", "pathname"), |
| 222 | + ("filename", "filename"), |
| 223 | + ("module", "module"), |
| 224 | + ("lineno", "lineno"), |
| 225 | + ("funcName", "funcName"), |
| 226 | + ("created", "created"), |
| 227 | + ("asctime", "asctime"), |
| 228 | + ("msecs", "msecs"), |
| 229 | + ("relativeCreated", "relativeCreated"), |
| 230 | + ("thread", "thread"), |
| 231 | + ("threadName", "threadName"), |
| 232 | + ("process", "process"), |
| 233 | + ("message", "message") |
| 234 | + ]) |
| 235 | + root = logging.getLogger() |
| 236 | + root.setLevel(logging.INFO) |
| 237 | + |
| 238 | + datefmt = None |
| 239 | + sh = logging.StreamHandler() |
| 240 | + formatter = JsonFormatter(ORDERED_DICT_FORMAT, datefmt) |
| 241 | + sh.setFormatter(formatter) |
| 242 | + |
| 243 | + sh.setLevel(logging.INFO) |
| 244 | + |
| 245 | + root.addHandler(sh) |
| 246 | + |
| 247 | + root.info("test %s format", 'ordered dict') |
| 248 | + |
| 249 | + def test_log_exception(self): |
| 250 | + root = logging.getLogger() |
| 251 | + root.setLevel(logging.INFO) |
| 252 | + |
| 253 | + sh = logging.StreamHandler() |
| 254 | + formatter = JsonFormatter() |
| 255 | + sh.setFormatter(formatter) |
| 256 | + |
| 257 | + sh.setLevel(logging.INFO) |
| 258 | + |
| 259 | + root.addHandler(sh) |
| 260 | + try: |
| 261 | + 1 / 0 |
| 262 | + except Exception as e: |
| 263 | + root.exception('test log exception') |
| 264 | + |
| 265 | + def test_record_custom_attrs(self): |
| 266 | + RECORD_CUSTOM_ATTRS = { |
| 267 | + 'asctime': lambda: datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S.%f'), |
| 268 | + 'user id': lambda: str(random.random())[2:10] |
| 269 | + } |
| 270 | + RECORD_CUSTOM_FORMAT = OrderedDict([ |
| 271 | + ("user id", "user id"), # new custom attrs |
| 272 | + ("name", "name"), |
| 273 | + ("levelno", "levelno"), |
| 274 | + ("levelname", "levelname"), |
| 275 | + ("pathname", "pathname"), |
| 276 | + ("filename", "filename"), |
| 277 | + ("module", "module"), |
| 278 | + ("lineno", "lineno"), |
| 279 | + ("funcName", "funcName"), |
| 280 | + ("created", "created"), |
| 281 | + ("asctime", "asctime"), # use custom format replace default. |
| 282 | + ("msecs", "msecs"), |
| 283 | + ("relativeCreated", "relativeCreated"), |
| 284 | + ("thread", "thread"), |
| 285 | + ("threadName", "threadName"), |
| 286 | + ("process", "process"), |
| 287 | + ("message", "message") |
| 288 | + ]) |
| 289 | + root = logging.getLogger() |
| 290 | + root.setLevel(logging.INFO) |
| 291 | + |
| 292 | + datefmt = None |
| 293 | + sh = logging.StreamHandler() |
| 294 | + formatter = JsonFormatter( |
| 295 | + RECORD_CUSTOM_FORMAT, datefmt, record_custom_attrs=RECORD_CUSTOM_ATTRS) |
| 296 | + sh.setFormatter(formatter) |
| 297 | + |
| 298 | + sh.setLevel(logging.INFO) |
| 299 | + |
| 300 | + root.addHandler(sh) |
| 301 | + root.info('test record custom attrs') |
| 302 | + |
| 303 | + def test_multi_value_in_one_key(self): |
| 304 | + MULTI_VALUE_FORMAT = { |
| 305 | + "multi value": "%(name)s - %(levelno)s - %(levelname)s - %(pathname)s - %(filename)s - %(module)s - %(lineno)d - %(funcName)s - %(created)f - %(asctime)s - %(msecs)d - %(relativeCreated)d - %(thread)d - %(threadName)s - %(process)d - %(message)s" |
| 306 | + } |
| 307 | + root = logging.getLogger() |
| 308 | + root.setLevel(logging.INFO) |
| 309 | + |
| 310 | + datefmt = None |
| 311 | + sh = logging.StreamHandler() |
| 312 | + formatter = JsonFormatter( |
| 313 | + MULTI_VALUE_FORMAT, datefmt) |
| 314 | + sh.setFormatter(formatter) |
| 315 | + |
| 316 | + sh.setLevel(logging.INFO) |
| 317 | + |
| 318 | + root.addHandler(sh) |
| 319 | + root.info('test multi value in one key') |
| 320 | + |
| 321 | + def test_json_dumps_parameter_indent(self): |
| 322 | + root = logging.getLogger() |
| 323 | + root.setLevel(logging.INFO) |
| 324 | + |
| 325 | + datefmt = None |
| 326 | + sh = logging.StreamHandler() |
| 327 | + formatter = JsonFormatter(indent=4) |
| 328 | + sh.setFormatter(formatter) |
| 329 | + |
| 330 | + sh.setLevel(logging.INFO) |
| 331 | + |
| 332 | + root.addHandler(sh) |
| 333 | + |
| 334 | + root.info('test json dumps parameter `index`: 4') |
| 335 | + |
| 336 | + def test_json_dumps_parameter_ensure_ascii_false(self): |
| 337 | + root = logging.getLogger() |
| 338 | + root.setLevel(logging.INFO) |
| 339 | + |
| 340 | + sh = logging.StreamHandler() |
| 341 | + formatter = JsonFormatter(ensure_ascii=False) |
| 342 | + sh.setFormatter(formatter) |
| 343 | + |
| 344 | + sh.setLevel(logging.INFO) |
| 345 | + |
| 346 | + root.addHandler(sh) |
| 347 | + |
| 348 | + root.info('test json dumps parameter `ensure_ascii` False: ÖÐÎÄ') |
| 349 | + |
| 350 | + def test_file_config(self): |
| 351 | + fileConfig(os.path.join(os.path.dirname( |
| 352 | + __file__), 'logger_config.ini')) |
| 353 | + root = logging.getLogger('root') |
| 354 | + root.info('test file config') |
| 355 | + |
| 356 | + def tearDown(self): |
| 357 | + root = logging.getLogger() |
| 358 | + # remove handlers |
| 359 | + root.handlers = [] |
| 360 | + |
| 361 | + |
| 362 | +if __name__ == '__main__': |
| 363 | + unittest.main() |
0 commit comments