Skip to content

Commit f0e7425

Browse files
committed
melhorias no processo de log com saida pelo docker
1 parent 0696a57 commit f0e7425

File tree

3 files changed

+76
-21
lines changed

3 files changed

+76
-21
lines changed

Dockerfile

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ RUN apt-get update && apt-get install -y nginx cron nano procps unzip git \
55

66
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
77

8+
# Configura o PHP-FPM para enviar logs para o coletor de logs do Docker
9+
RUN ln -sf /dev/stdout /var/log/php-fpm.access.log \
10+
&& ln -sf /dev/stderr /var/log/php-fpm.error.log
11+
12+
# Configura o log de erros do PHP
13+
RUN echo "error_log = /dev/stderr" >> /usr/local/etc/php/php.ini
14+
815
COPY default.conf /etc/nginx/sites-available/default
916

1017
RUN mkdir -p /app
@@ -19,11 +26,16 @@ RUN chmod +x /usr/local/bin/docker-entrypoint.sh
1926

2027
RUN mkdir -p /app/logs
2128

22-
RUN touch /app/logs/cron.log
23-
RUN echo '0 * * * * root php "/app/cron/fetchFeeds.php" >> /app/logs/cron.log 2>&1' >> /etc/crontab
29+
# Configura o cron para usar o logger para coleta de logs do Docker
30+
RUN touch /app/logs/cron.log && ln -sf /dev/stdout /app/logs/cron.log
31+
RUN echo '0 * * * * root php "/app/cron/fetchFeeds.php" 2>&1 | logger -t cron-fetchfeeds' >> /etc/crontab
2432

2533
RUN chown -R www-data:www-data /app && chmod -R 755 /app
2634

35+
# Garante que os logs do nginx sejam direcionados para o coletor de logs do Docker
36+
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
37+
&& ln -sf /dev/stderr /var/log/nginx/error.log
38+
2739
EXPOSE 80
2840

2941
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]

default.conf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ server {
77

88
server_name _;
99

10+
access_log /dev/stdout;
11+
error_log /dev/stderr;
12+
1013
location / {
1114
try_files $uri $uri/ /index.php?$query_string;
1215
}
@@ -23,12 +26,12 @@ server {
2326
try_files $uri $uri/ /admin.php?$query_string;
2427
}
2528

26-
location ^~ /logs/ {
29+
location ^~ /logs/ {
2730
deny all;
2831
return 403;
2932
}
3033

31-
location ^~ /cron/ {
34+
location ^~ /cron/ {
3235
deny all;
3336
return 403;
3437
}
@@ -41,6 +44,4 @@ server {
4144
location ~ /\.ht {
4245
deny all;
4346
}
44-
45-
access_log /dev/null;
4647
}

docker-entrypoint.sh

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ NC='\033[0m' # No Color
1717

1818
# Função para logs de sucesso
1919
log_success() {
20-
echo -e "${GREEN}[✓] $1${NC}"
20+
echo -e "${GREEN}[✓] $1${NC}" | tee -a /dev/stdout
2121
}
2222

2323
# Função para logs de erro
2424
log_error() {
25-
echo -e "${RED}[✗] $1${NC}"
25+
echo -e "${RED}[✗] $1${NC}" | tee -a /dev/stderr
2626
exit 1
2727
}
2828

2929
# Função para logs de informação
3030
log_info() {
31-
echo -e "${YELLOW}[i] $1${NC}"
31+
echo -e "${YELLOW}[i] $1${NC}" | tee -a /dev/stdout
3232
}
3333

34-
echo -e "\n${YELLOW}=== Iniciando Container Sintoniza ===${NC}\n"
34+
echo -e "\n${YELLOW}=== Iniciando Container Sintoniza ===${NC}\n" | tee -a /dev/stdout
3535

3636
# === Validação de Variáveis de Ambiente ===
3737
log_info "Validando variáveis de ambiente..."
@@ -73,12 +73,56 @@ echo "ADMIN_PASSWORD=${ADMIN_PASSWORD}" >> /app/.env
7373

7474
log_success "Variáveis de ambiente configuradas"
7575

76+
# === Configuração de Logs ===
77+
log_info "Configurando sistema de logs..."
78+
79+
# Ensure log directories exist with proper permissions
80+
mkdir -p /var/log/nginx
81+
mkdir -p /var/log/php-fpm
82+
chown -R www-data:www-data /var/log/nginx /var/log/php-fpm
83+
84+
# Configure logrotate for nginx
85+
cat > /etc/logrotate.d/nginx << EOF
86+
/var/log/nginx/*.log {
87+
daily
88+
missingok
89+
rotate 7
90+
compress
91+
delaycompress
92+
notifempty
93+
create 0640 www-data adm
94+
sharedscripts
95+
postrotate
96+
[ ! -f /var/run/nginx.pid ] || kill -USR1 \`cat /var/run/nginx.pid\`
97+
endscript
98+
}
99+
EOF
100+
101+
# Configure logrotate for PHP-FPM
102+
cat > /etc/logrotate.d/php-fpm << EOF
103+
/var/log/php-fpm/*.log {
104+
daily
105+
missingok
106+
rotate 7
107+
compress
108+
delaycompress
109+
notifempty
110+
create 0640 www-data adm
111+
sharedscripts
112+
postrotate
113+
[ ! -f /var/run/php-fpm.pid ] || kill -USR1 \`cat /var/run/php-fpm.pid\`
114+
endscript
115+
}
116+
EOF
117+
118+
log_success "Sistema de logs configurado"
119+
76120
# === Inicialização dos Serviços ===
77-
echo -e "\n${YELLOW}=== Iniciando serviços ===${NC}\n"
121+
echo -e "\n${YELLOW}=== Iniciando serviços ===${NC}\n" | tee -a /dev/stdout
78122

79-
# Iniciando Cron
123+
# Iniciando Cron com redirecionamento de logs
80124
log_info "Iniciando serviço Cron..."
81-
service cron restart
125+
service cron restart 2>&1 | logger -t cron
82126
log_success "Serviço Cron iniciado"
83127

84128
# Funções de verificação de serviços
@@ -106,28 +150,26 @@ if [ ! -d /var/run/php ]; then
106150
log_success "Diretório PHP-FPM criado"
107151
fi
108152

109-
# Iniciando PHP-FPM
153+
# Iniciando PHP-FPM com redirecionamento de logs
110154
log_info "Iniciando PHP-FPM..."
111-
php-fpm &
155+
php-fpm --allow-to-run-as-root 2>&1 | logger -t php-fpm &
112156
sleep 3
113157
check_php_fpm
114158

115159
# Verificando configuração Nginx
116160
log_info "Verificando configuração do Nginx..."
117-
nginx -t
161+
nginx -t 2>&1 | logger -t nginx-config
118162
if [ $? -ne 0 ]; then
119163
log_error "Configuração do Nginx inválida"
120164
else
121165
log_success "Configuração do Nginx válida"
122166
fi
123167

124-
# Iniciando Nginx
168+
# Iniciando Nginx em primeiro plano com redirecionamento de logs
125169
log_info "Iniciando Nginx..."
126-
nginx -g "daemon off;" &
127-
sleep 3
128-
check_nginx
170+
exec nginx -g "daemon off;" 2>&1 | logger -t nginx
129171

130-
echo -e "\n${GREEN}=== Container Sintoniza inicializado ===${NC}\n"
172+
echo -e "\n${GREEN}=== Container Sintoniza inicializado ===${NC}\n" | tee -a /dev/stdout
131173

132174
wait -n
133175

0 commit comments

Comments
 (0)