Search code examples
python-3.xsanic

How to change the default sanic log directory to a custom directory?


Sanic currently supports Linux log directory as /dev/log, and the log wouldn't work if the directory does not exist. How do I change the directory to a custom one?


Solution

  • After going through documentation and trying various combination I found the following solution to be working, use the following dict and change value of file path were ever neccessary.

    from sanic.log import DefaultFilter
    import sys
    LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'accessFilter': {
            '()': DefaultFilter,
            'param': [0, 10, 20]
        },
        'errorFilter': {
            '()': DefaultFilter,
            'param': [30, 40, 50]
        }
    },
    'formatters': {
        'simple': {
            'format': '%(asctime)s - (%(name)s)[%(levelname)s]: %(message)s',
            'datefmt': '%Y-%m-%d %H:%M:%S'
        },
        'access': {
            'format': '%(asctime)s - (%(name)s)[%(levelname)s][%(host)s]: ' +
                      '%(request)s %(message)s %(status)d %(byte)d',
            'datefmt': '%Y-%m-%d %H:%M:%S'
        }
    },
    'handlers': {
        'internalFile': {
            'class': 'logging.FileHandler',
            'filters': ['accessFilter'],
            'formatter': 'simple',
            'filename': "temp/clickinternal.log"
        },
        'accessFile': {
            'class': 'logging.FileHandler',
            'filters': ['accessFilter'],
            'formatter': 'access',
            'filename': "temp/clickaccess.log"
        },
        'errorFile': {
            'class': 'logging.FileHandler',
            'filters': ['errorFilter'],
            'formatter': 'simple',
            'filename': "temp/clickerr.log"
        },
        'internal': {
            'class': 'logging.StreamHandler',
            'filters': ['accessFilter'],
            'formatter': 'simple',
            'stream': sys.stderr
        },
        'accessStream': {
            'class': 'logging.StreamHandler',
            'filters': ['accessFilter'],
            'formatter': 'access',
            'stream': sys.stderr
        },
        'errorStream': {
            'class': 'logging.StreamHandler',
            'filters': ['errorFilter'],
            'formatter': 'simple',
            'stream': sys.stderr
        }
    },
    'loggers': {
        'sanic': {
            'level': 'DEBUG',
            'handlers': ['internal','errorStream','internalFile', 'errorFile']
        },
        'network': {
            'level': 'DEBUG',
            'handlers': ['accessStream','errorStream','accessFile', 'errorFile']
        }
    }
    }
    
    
    
    app.run(debug=True,log_config=LOGGING, host='0.0.0.0', port='8001')