javapenguin
javapenguin

Reputation: 1036

Include date in nginx log file name

I am running nginx 1.15.6 and I am trying to include the current date in the nginx log file name.

Some thing like this: access_log /var/log/nginx/access.2018.11.07.log main;

Anyone know how to do this?

Upvotes: 5

Views: 11841

Answers (4)

Giangimgs
Giangimgs

Reputation: 1200

I use crontab to run a shell script every 0h 1m.

Structure of the form:

~/home/
    log/
        nginx/
            access/
                2023-10-03.log
                2023-10-02.log
                2023-10-01.log
                ...
            errors/
                2023-10-03.log
                2023-10-02.log
                2023-10-01.log
                ...

Concept: Create a config file with the date, then include the config file in the server block of domain config

Domain config in /etc/nginx/conf.d

server {    
    listen 80;
    include /etc/nginx/log_by_date.conf;    
    root /.../;
...
}

Shell script update_log_by_date.sh

#! /bin/bash
date=$(date -Id)
home_folder="/home"
nginx_logs="$home_folder/logs/nginx"

# Create log folder if it does not exist
mkdir -p "$nginx_logs/access"
mkdir -p "$nginx_logs/errors"

# Create log file if does not exist
accessFile="$nginx_logs/access/${date}.log"
test -f $accessFile || touch $accessFile

errorFile="$nginx_logs/errors/${date}.log"
test -f $errorFile || touch $errorFile

# Set permission
chown -R nginx:nginx $nginx_logs

# Overwrite config
echo "access_log $nginx_logs/access/${date}.log;" > $home_folder/log_by_date.conf
echo "error_log $nginx_logs/errors/${date}.log warn;" >> $home_folder/log_by_date.conf

# Copy config file to nginx folder
sudo cp $home_folder/log_by_date.conf /etc/nginx

# Restart nginx to update config
sudo systemctl restart nginx

Use crontab to run the script everyday at 0h1m 1 0 * * * sh /folder/update_log_by_date.sh

Upvotes: 0

Miles Prower
Miles Prower

Reputation: 105

IF is Evil, in Nginx configurations. A detailed description as to why can be found at Nginx: If is Evil.

A better solution would be to use a map for logging dates.

Example below.

map $time_iso8601 $year {
    default             'date';
    '~^(?<yyyy>\d{4})-'     $yyyy;
}
map $time_iso8601 $month {
    default             'not';
    '~^\d{4}-(?<mm>\d{2})-'     $mm;
}
map $time_iso8601 $day {
    default             'found';
    '~^\d{4}-\d{2}-(?<dd>\d{2})'    $dd;
}
map $time_iso8601 $logfile_date {
    default                         'date-not-found';
    '~^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})'    $year-$month-$day;
}

access_log c:/tools/nginx/logs/$logfile_date.access.log;

Upvotes: 0

javapenguin
javapenguin

Reputation: 1036

This is what I ended up using and it works perfectly:

map $time_iso8601 $year {
    default '0000';
    "~^(\d{4})-(\d{2})-(\d{2})" $1;
}
map $time_iso8601 $month {
    default '00';
    "~^(\d{4})-(\d{2})-(\d{2})" $2;
}
map $time_iso8601 $day {
    default '00';
    "~^(\d{4})-(\d{2})-(\d{2})" $3;
}

access_log  /var/log/nginx/access.$year-$month-$day.log  apm_json;

Upvotes: 8

javapenguin
javapenguin

Reputation: 1036

server {

    if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})") {
        set $year $1;
        set $month $2;
        set $day $3;
    }

    access_log /var/log/nginx/$year-$month-$day-access.log;

Upvotes: 2

Related Questions