Skip to content

Commit e3ba93f

Browse files
committed
Improved daily rotation of Pino logs without using log rotate
1 parent 6532b02 commit e3ba93f

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

docker/configuration/logrotate.conf

Lines changed: 0 additions & 10 deletions
This file was deleted.

docker/scripts/Dockerfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
FROM node:lts-alpine
66

77
# Install system dependencies
8-
RUN apk add --no-cache logrotate openssl supercronic tzdata
8+
RUN apk add --no-cache openssl supercronic tzdata
99

1010
# Set the working directory to the website files
1111
WORKDIR /usr/src/app
@@ -29,7 +29,7 @@ RUN --mount=type=cache,target=.npm \
2929
RUN npx tsc --skipLibCheck scripts/create-fake-accounts.ts scripts/expired-files.ts scripts/outdated-notifications.ts && \
3030
echo "*/5 * * * * node /usr/src/app/scripts/expired-files.js > /dev/null 2>&1" >> /var/spool/cron/crontabs/node && \
3131
echo "*/5 * * * * node /usr/src/app/scripts/outdated-notifications > /dev/null 2>&1" >> /var/spool/cron/crontabs/node && \
32-
echo "0 0 * * * logrotate -f /usr/src/app/docker/configuration/logrotate.conf -s /usr/src/app/docker/configuration/logrotate.status > /dev/null 2>&1" >> /var/spool/cron/crontabs/node
32+
echo "0 0 * * * /usr/src/app/docker/scripts/logrotate.sh > /dev/null 2>&1" >> /var/spool/cron/crontabs/node
3333

3434
# Add wait script to wait for other services to be ready
3535
ADD --chmod=0755 https://github.com/ufoscout/docker-compose-wait/releases/latest/download/wait /wait
@@ -39,5 +39,4 @@ USER node
3939

4040
# Prepare project files and configuration to be built
4141
RUN npm run build && npm prune --production && \
42-
chmod +x ./docker/scripts/entrypoint.sh && \
43-
chmod 644 /usr/src/app/docker/configuration/logrotate.conf
42+
chmod +x ./docker/scripts/entrypoint.sh ./docker/scripts/logrotate.sh

docker/scripts/logrotate.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/sh
2+
3+
echo "Copying application.log to $(date +"%Y-%m-%d").log..."
4+
5+
# Go to logs directory
6+
cd logs
7+
8+
# Copy content from source file to dated log file
9+
cp "application.log" "$(date +"%Y-%m-%d").log"
10+
11+
# Clear the content of the source file
12+
> "application.log"
13+
echo "Source log has been cleared."
14+
15+
# Get list of .log files, excluding application.log
16+
LOG_FILES=$(ls -1 *.log 2>/dev/null | grep -v "^application.log$")
17+
18+
# Count number of log files
19+
LOG_COUNT=$(echo "$LOG_FILES" | wc -l)
20+
21+
echo "$LOG_COUNT log files found."
22+
23+
# Keep only the 14 newest by filename (sorted alphabetically, oldest first)
24+
if [ "$LOG_COUNT" -gt 14 ]; then
25+
FILES_TO_DELETE=$(echo "$LOG_FILES" | sort | head -n $(($LOG_COUNT - 14)))
26+
27+
echo "Deleting old log files:"
28+
29+
for file in $FILES_TO_DELETE; do
30+
echo " - Deleting $file"
31+
rm -f "$file"
32+
done
33+
else
34+
echo "No log files to delete. Keeping all $LOG_COUNT files."
35+
fi
36+
37+
echo "Done."

0 commit comments

Comments
 (0)