Skip to content

Instantly share code, notes, and snippets.

@ColinSullivan1
Last active February 8, 2019 16:51
Show Gist options
  • Save ColinSullivan1/6f4c8460905acefcbd969d99ec5ff347 to your computer and use it in GitHub Desktop.
Save ColinSullivan1/6f4c8460905acefcbd969d99ec5ff347 to your computer and use it in GitHub Desktop.
Rotating NATS server files on windows

Rotating log files in Windows

Here's a quick description of how to rotate log files on windows. In order to do so, your NATS server process must be running as a windows service. Here's an example setup and minimal config to setup log rotation.

The overall process is straightforward - when the server is running, change the name of the configuration file in your server config, and trigger a server reload. The server will close the old file and reopen the log in the new file.

First, we'll need to setup a few configuration files. For convenience, (you'll see later) include a log.conf (or equivalient) that has your log file parameter.

In these examples, we need fully qualified directories - substitute c:\users\colin\lr with your directory.

C:\Users\colin\lr>dir /b
gnatsd.exe
log.conf
rotate.bat
server.conf

Configuration files

server.conf

# Your server settings

include "log.conf"

log.conf

logfile="c:/users/colin/lr/server1.log"

Setting up the service

sc.exe create nats-server binPath= "c:\users\colin\lr\gnatsd.exe -c c:\users\colin\lr\server.conf"
sc.exe start nats-server

Rotating the logs

rotate.bat

To rotate the log, replace the contents of the log file with your new log filename, and then use gnatsd.exe -sl reload=<service name> to reload.

Here's a simple script to replace the contents of log.conf and trigger a server reload.

echo logfile=C:/Users/colin/lr/%1 > log.conf
gnatsd.exe -sl reload=nats-server

Log rotation in action

C:\Users\colin\lr>type server1.log
[6412] 2019/02/08 09:24:21.842083 [INF] Starting nats-server version 2.0.0-RC3
[6412] 2019/02/08 09:24:21.843561 [INF] Git commit [not set]
[6412] 2019/02/08 09:24:21.844627 [INF] Listening for client connections on 0.0.0.0:4222
[6412] 2019/02/08 09:24:21.844627 [INF] Server id is NCOOIEJ673INGI3RTU7WKL74WOJRIZKWZSTVJK2CLFRIBEL2KKV6JCJM
[6412] 2019/02/08 09:24:21.844627 [INF] Server is ready

C:\Users\colin\lr>rotate server2.log

C:\Users\colin\lr>echo logfile=C:/Users/colin/lr/server2.log  1>log.conf

C:\Users\colin\lr>gnatsd.exe -sl reload=nats-server

C:\Users\colin\lr>type server1.log
[6412] 2019/02/08 09:24:21.842083 [INF] Starting nats-server version 2.0.0-RC3
[6412] 2019/02/08 09:24:21.843561 [INF] Git commit [not set]
[6412] 2019/02/08 09:24:21.844627 [INF] Listening for client connections on 0.0.0.0:4222
[6412] 2019/02/08 09:24:21.844627 [INF] Server id is NCOOIEJ673INGI3RTU7WKL74WOJRIZKWZSTVJK2CLFRIBEL2KKV6JCJM
[6412] 2019/02/08 09:24:21.844627 [INF] Server is ready
[6412] 2019/02/08 09:42:14.039636 [INF] Reloaded: log_file = C:/Users/colin/lr/server2.log

C:\Users\colin\lr>type server2.log
[6412] 2019/02/08 09:42:14.047707 [INF] Reloaded server configuration

C:\Users\colin\lr>dir /b
gnatsd.exe
log.conf
rotate.bat
server.conf
server1.log
server2.log

C:\Users\colin\lr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment