How to logging?

I start syncthing via rc.local, with this:

nice -n 19 ionice -c3 su -l syncthing -c "/home/syncthing/bin/syncthing" &

But what’s about logging?

I can redirect stdout and stderr into a file, maybe in /var/log/syncthing But then, the file will get bigger and bigger, isn’t it?

So i make currently this:

nice -n 19 ionice -c3 su -l syncthing -c "/home/syncthing/bin/syncthing" >> "/var/log/syncthing_$(date +"%Y%m")" 2>&1 &

So i will get one log file for one month…

Any better idea? Should syncthing implement a own file logging feature with a log-rotate?

You could pipe to logger and let whatever syslog system is in place do the rotation.

Can you give a example?

Sure. This is a start script I use with svrun on one of my machines;

#!/bin/sh

export HOME=/home/jb
export STNORESTART=1
setuidgid jb /home/jb/bin/syncthing 2>&1 | logger -t syncthing

When this runs, log output gets sent to regular syslog and can be handled by whatever syslog mechanisms you have in place.

jb@syncer:~$ sudo tail -f /var/log/syslog
May 21 14:06:15 syncer syncthing: [LGFPD] 14:06:15 INFO: syncthing v0.8.7-18-ge9eacfa (go1.2.2 linux-amd64) jb@jborg-mbp 2014-05-19 22:59:19 UTC
May 21 14:06:15 syncer syncthing: [LGFPD] 14:06:15 INFO: My ID: LGFPDIT7SKNNJVJZA4FC7QNCRKCE753K72BW5QD2FOZ7FRFEP57Q
May 21 14:06:15 syncer syncthing: [LGFPD] 14:06:15 INFO: Starting web GUI on http://0.0.0.0:8080/
May 21 14:06:15 syncer syncthing: [LGFPD] 14:06:15 INFO: Populating repository index
May 21 14:06:21 syncer syncthing: [LGFPD] 14:06:21 INFO: No UPnP IGD device found, no port mapping created (read udp4 0.0.0.0:46581: i/o timeout)
May 21 14:06:21 syncer syncthing: [LGFPD] 14:06:21 INFO: Sending local discovery announcements

We get a double timestamp, one from syncthing and one from syslog. Could have a flag to disable that in case the log data is timestamped externally but I haven’t been sufficiently annoyed by it yet.

2 Likes

I will try, thanks!

And yes, a flag for no timestamps would be good!

Hello there and on Windows?

Hello, Is there a way to get logger to log only certain information. Syncthing has log level in tags: INFO, OK etc.

do you know if there is a way, to force logger to get only WARNING message etc.

If you are on unix, you can pipe it through grep I guess.

1 Like

Is there a way to pipe to “logger -t syncthing” in a init.d deamon script, that will work correctly both when used with “service start/stop” as well as when syncthing is restarting due to configuration change? I’m on debian (raspberry) and Ubuntu 14.4. Havent been able to make it work so far.

look at this https://github.com/jedie/syncthing/wiki/Syncthing-in-Synology-DSM

I’ve achieved this by creating script in middle of start-stop-startus app and syncthing. This way daemon restarts also logger.

Thanks, I updated the script to my raspberry pi raspian setup, and inserted a missing function (daemon_status), please see below, this has now been tested both with service start/stop/status, syncthing restart from GUI after config update, syncthing version auto update and rebooting. Hope it will help someone: /etc/init.d script (named “syncthing”):

#!/bin/sh 
### BEGIN INIT INFO
# Provides: syncthing
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: daemonized version of syncthing.
# Description: Starts the syncthing daemon for user root
### END INIT INFO

# Package
PACKAGE="syncthing"
DNAME="Syncthing"

# Others
INSTALL_DIR="/usr/local/bin"
PATH="${INSTALL_DIR}:${PATH}"
USER="root"

SYNCTHING="${INSTALL_DIR}/syncthing.sh" 
SYNCTHINGAPP="${INSTALL_DIR}/syncthing"

daemon_status () 
{
   start-stop-daemon --start --test --exec ${SYNCTHINGAPP} >/dev/null 2>&1
    if [ $? -eq 0 ]; then 
        return 1 
    else 
        return 0 
    fi
}

start_daemon ()
{
    start-stop-daemon -b -o -c ${USER} -S -u ${USER} -x ${SYNCTHING} 
}


stop_daemon ()
{
    start-stop-daemon -o -c ${USER} -K -u ${USER} -x ${SYNCTHINGAPP}
}


case $1 in
    start)
        if daemon_status; then
            echo ${DNAME} is already running
        else
            echo Starting ${DNAME} ...
            start_daemon
        fi
        ;;
    stop)
        if daemon_status; then
            echo Stopping ${DNAME} ...
            stop_daemon
        else
            echo ${DNAME} is not running
        fi
        ;;
    status)
        if daemon_status; then
            echo ${DNAME} is running
            exit 0
        else
            echo ${DNAME} is not running
            exit 1
        fi
        ;;
    log)
        exit 1
        ;;
    *)
        exit 1
        ;;
esac

I have then created the following helping script to start syncthing and pipe output the the syslog daemon using the “logger” command. Script called “syncthing.sh” placed with syncthing binary:

#!/bin/sh
/usr/local/bin/syncthing 2>&1 | logger -t syncthing
1 Like