Search code examples
pythonapacheloggingfalconframework

Python - Different logs should be in different files, but appear on the same file


I have a web application, with a minimal logging functionality. The backend runs on Apache with Falcon (on Python).

Each request has the following code

msg = 'user: {usr} running {req} {req_uri:<30} | from: {loc_ip}:{loc_port} '.format(
        usr=req.env['REMOTE_USER'],
        req=req.env['REQUEST_METHOD'],
        loc_ip=req.env['REMOTE_ADDR'],
        loc_port=req.env['REMOTE_PORT'],
        req_uri=req.env['REQUEST_URI'])

    log_name = 'logs/{remote_ip}/{remote_ip}_{day}.log'.format(remote_ip=req.env['REMOTE_ADDR'], day=datetime.datetime.now().date().strftime('%d_%m_%Y'))
    os.makedirs(os.path.dirname(log_name), exist_ok=True)
    logging.basicConfig(filename=log_name, level=logging.DEBUG, format='[%(asctime)s] - %(levelname)s - [%(module)s:%(lineno)d] %(message)s', datefmt='%d/%m/%Y %H:%M:%S')
    logging.info(msg)

And I access the app from 2 different IPs (IPx, and IPy) - so there should be 2 folder under 'logs', folder 'IPx' and folder 'IPy', with a log file in each..

But after I access the web, I see 2 folders, but only one of the folders has a log file inside (lets say IPx_day.log in IPx folder), but after reviewing the IPx_day.log file I see:

[TIME] - INFO - [FILE] user: user1 running GET /domain | from: IPy:PORTy
// Other log statments..
[TIME] - INFO - [FILE] user: user2 running GET /domain | from: IPx:PORTx

both lines are in the file file - IPx_day.log

Has anyone have idea why both lines appear in the same file?

Thanks.


Solution

  • [I would add this as a comment but I don't have the reputation to do so.]

    I think it might have something to do with logging.basicConfig() which only configures the root logger. I think you'd have to use multiple loggers or better just attach multiple handlers to one logger.

    https://docs.python.org/3/library/logging.html#logging.basicConfig https://docs.python.org/3/library/logging.handlers.html