Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: Gavin Andresen on July 12, 2010, 04:39:47 PM



Title: Don't forget to rotate your logs...
Post by: Gavin Andresen on July 12, 2010, 04:39:47 PM
Reminder to anybody running a bitcoind server:   be sure the debug.log isn't filling up your server's disk.  With the slashdotting, now might be a good time to setup a debug.log housekeeping system.

I'm doing this on my Debian server:

My crontab:
Code:
# Rotate bitcoin logs
0 8 * * * /usr/sbin/logrotate --state /home/gavin/.bitcoin/logrotate.state /home/gavin/bc_logrotate.conf
My bc_logrotate.conf file:
Code:
# 
# Rotate the bitcoin debug.log file
#
# This should be added to crontab to be run every day:
#  /usr/sbin/logrotate /path/to/bc_logrotate.conf
#
compress
copytruncate

/home/gavin/.bitcoin/debug.log {
rotate 5
}


Title: Re: Don't forget to rotate your logs...
Post by: lachesis on July 12, 2010, 10:15:33 PM
Great idea, Gavin. Thanks for the code; I'll copy that exactly (except for the gavin part :))


Title: Re: Don't forget to rotate your logs...
Post by: Gavin Andresen on November 18, 2010, 12:18:11 AM
I just committed a change to svn to the way debug.log is written that means you need to change your logrotate configuration.

Why the change:  before, bitcoin was opening and closing debug.log on every print.  That caused it to be unusable on Macs running FileVault (and slower than it aught to be on various other systems).

After subversion rev 183, bitcoin will keep debug.log open and just append to it.

If you're rotating logs using the unix logrotate command, just add 'copytruncate' to its configuration file and the right thing will happen.  I changed the example in my post above.


Title: Re: Don't forget to rotate your logs...
Post by: theymos on November 18, 2010, 12:27:37 AM
Bitcoin already limits the size of debug.log.


Title: Re: Don't forget to rotate your logs...
Post by: throughput on November 18, 2010, 08:07:09 AM
I just committed a change to svn to the way debug.log is written that means you need to change your logrotate configuration.

Why the change:  before, bitcoin was opening and closing debug.log on every print.  That caused it to be unusable on Macs running FileVault (and slower than it aught to be on various other systems).

After subversion rev 183, bitcoin will keep debug.log open and just append to it.

If you're rotating logs using the unix logrotate command, just add 'copytruncate' to its configuration file and the right thing will happen.  I changed the example in my post above.

Oh, my dear, why not you just haven't invented some new rpc command, like "reopenlogs" ?


Title: Re: Don't forget to rotate your logs...
Post by: dmp1ce on April 11, 2011, 01:26:24 PM
Bitcoin already limits the size of debug.log.

What does Bitcoin limit the log size to?  I keep losing most of my log data and I'm wondering if Bitcoin keeps deleting it and starting over.


Title: Re: Don't forget to rotate your logs...
Post by: dmp1ce on April 11, 2011, 02:05:08 PM
Nevermind, I think I found it.  It looks like 10 MB is when Bitcoin decides to reset the debug.log.

Code:
void ShrinkDebugFile()
{
    // Scroll debug.log if it's getting too big
    string strFile = GetDataDir() + "/debug.log";
    FILE* file = fopen(strFile.c_str(), "r");
    if (file && GetFilesize(file) > 10 * 1000000)
    { 
        // Restart the file with some of the end
        char pch[200000];
        fseek(file, -sizeof(pch), SEEK_END);
        int nBytes = fread(pch, 1, sizeof(pch), file);
        fclose(file);
        if (file = fopen(strFile.c_str(), "w"))
        { 
            fwrite(pch, 1, nBytes, file);
            fclose(file);
        }
    }
}


Title: Re: Don't forget to rotate your logs...
Post by: theymos on April 12, 2011, 11:57:19 AM
It only does it on startup, so the file might actually get bigger than 10MB.


Title: Re: Don't forget to rotate your logs...
Post by: M4v3R on April 12, 2011, 10:35:36 PM
Yes, mine was around 100 MB after a longer while. Thanks for the logrotate tip, now it's back to normal :).


Title: Re: Don't forget to rotate your logs...
Post by: error on July 27, 2011, 08:47:40 PM
Or perhaps reopen the log on SIGHUP, like most other services?


Title: Re: Don't forget to rotate your logs...
Post by: phillipsjk on February 23, 2014, 07:08:04 PM
FreeBSD uses a log rotator called newsyslog.

The following line in /etc/newsyslog.conf appears to do the right thing:
Code:
/home/bitcoin/debug.log bitcoin:bitcoin 644  5     100  *  JR /home/bitcoin/sighup.sh

The fields are separated by white-space. They are: 'logfile_name', 'owner:group', 'mode', 'count' (number of archives to keep), 'size' (in kb), 'when' (chose anytime), 'flags' (J->Bzip2, R-> Treat next field as as shell command to run), 'path_to_pid_cmd_file' (filename containing daemon's PID, must start with /), 'signal_number' (optional, omitted in example).

I first tried appending "killall -HUP bitcoind" directly, but the arguments were interpreted as an invalid signal number. With the size limited to 100 kB, the logs get rotated every hour (because they they grow faster than that).

sighup.sh is just a simple shell-script:
Code:
#!/bin/sh
#Sends bitcoind a Sighup.
killall -HUP bitcoind


I was not able to find any 'copytruncate', setting, but it appears to do the right thing.