Answer a question

I try to implement logging in my django project (Django/Postgresql/Docker/Celery) but got an error when I deploy my project in linux server (but it work locally)

FileNotFoundError: [Errno 2] No such file or directory: '/usr/src/app/logs/debug.log'

I've read about a solution in this SO post: Django: Unable to configure handler 'syslog'

but fisrt I do not even understand why this solution should works and I do not manage to implement it in my project. I use Docker/docker-compose so I install netcat with Dockerfile and then try to RUN nc -lU /logs/debug.log /logs/info.log but it failed (no such file or directory)

Dockerfile

# Pull the official base image
FROM python:3.8.3-alpine

# Set a work directory
WORKDIR /usr/src/app

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 dependencies
RUN apk update && apk add postgresql-dev gcc g++ python3-dev musl-dev netcat-openbsd

# create UNIX socket for logs files
#RUN nc -lU /logs/debug.log /logs/info.log
...

settings.py

...
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'loggers': {
        'django': {
            'handlers':['files_info','files_debug'],
            'propagate': True,
            'level':'INFO',
        },
    },
    'handlers': {
        'files_info': {
            'level': 'INFO',
            'class': 'logging.FileHandler',
            'filename': './logs/info.log',
            'formatter': 'mereva',
        },
        'files_debug': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': './logs/debug.log',
            'formatter': 'mereva',
        },
    },
    'formatters': {
        'mereva': {
            'format': '{levelname} {asctime} {module} {message}',
            'style': '{',
        }
    },
}
...

Answers

In fact, logs folder and .log files were excluded with my .gitignore files. Changing to logsfiles folder and debug and info (without extension) files, resolve the problem and I was able to run container

neverthless, nothing is written in theses files...

Logo

云原生社区为您提供最前沿的新闻资讯和知识内容

更多推荐