sparcs-kaist / sparcs09

SPARCS 공동구매 시스템 https://09.sparcs.org/
MIT License
2 stars 2 forks source link

add custom logger; add logging to /sessions and /users api (done #6) #39

Closed samjo-nyang closed 7 years ago

samjo-nyang commented 7 years ago

Changes:

  1. added DBHandler in apps/logger.py
  2. added collect-logs custom django script
  3. added send-mail custom django script

Features:

  1. automatically adds logs to both databases and files when logger.*** called
    • logs are saved as YYYYMMDD.pid.log format in LOG_BUFFER_DIR
  2. automatically merge log files when collect-logs command is executed
    • logs are merged as YYYYMMDD.log format in LOG_DIR
    • old (> 30 days) logs are deleted from the DB
  3. send mail for high-level logs
    • if DEBUG is false, high-level logs are sent to admin email
    • high-levels are warning, error and critical-level

Usages:

  1. add followings to the top of the views (or backends) file
    import logging
    from apps.core.models import UserLog
    logger = logging.getLogger(UserLog.GROUP_XXX)
  2. use like logger.info('login.try', {'r': request: 'uid': 'abcedf', 'extra': {'a': 1, 'b': 2}, 'hide': True})
    • r: pass the request object itself to detect the user or/and the ip
    • uid: use iff the user is not logged in (so the module cannot resolve user from request object)
      • it should be a username (of User object)
    • extra: give an extra information as dictionary
      • it will be converted into key1=value1, key2=value2, ... format
      • it will be appended after the log message
    • hide: optional flags that denotes this log is hidden to users (default is false)
  3. use only info, warn, error and critical level
  4. the log will be recorded as (UserLog.GROUP_XXX).login.try: a=1, b=2