nats-server icon indicating copy to clipboard operation
nats-server copied to clipboard

Embedded nats servers with opt.LogFile have no logging

Open ZiRunHua opened this issue 1 year ago • 2 comments

Observed behavior

Use an embedded nats server with opt.LogFile but logging is not logged

Expected behavior

The expected behavior is to see logging content in the specified log file, such as the following: [1] 2024/05/13 11:48:57.455246 [TRC] 127.0.0.1:48990-cid :5 - <<- [SUB statisticUpdate 1]

Server and client version

tested with nats-server v2.10.12 and nats v1.33.1

Host environment

linux

Steps to reproduce

	natsLogPath := "log/nats.log"
	opts := &server.Options{LogFile: natsLogPath}
	nastServer, err := server.NewServer(opts)
	if err != nil {
		panic(err)
	}
	nastServer.Start()
	Nats, err = nats.Connect(nats.DefaultURL)
	if err != nil {
		panic(err)
	}

ZiRunHua avatar May 13 '24 06:05 ZiRunHua

I am not sure if this is a problem with options, I tried to add other options, but the log is still not recorded. After that, I found the SetLoggerV2 method, which solved the problem for the time being.

nastServer.SetLoggerV2(logger.NewFileLogger(natsLogPath, true, false, true, true, logger.LogUTC(false)), false, true, false)

Now that the presentation has solved the problem, the log is recorded:

[1] 2024/05/13 14:38:27.067480 [TRC] 127.0.0.1:49120 - cid:5 - <<- [SUB statisticUpdate  1]
[1] 2024/05/13 14:38:36.644184 [TRC] 127.0.0.1:49120 - cid:5 - <<- [PUB statisticUpdate 134]
[1] 2024/05/13 14:38:36.644204 [TRC] 127.0.0.1:49120 - cid:5 - <<- MSG_PAYLOAD: ["{\"AccountId\":25,\"UserId\":24,\"IncomeExpense\":\"expense\",\"CategoryId\":615,\"TradeTime\":\"2024-05-13T10:24:52+08:00\",\"Amount\":800,\"Count\":1}"]
[1] 2024/05/13 14:38:36.644216 [TRC] 127.0.0.1:49120 - cid:5 - ->> [MSG statisticUpdate 1 134]

ZiRunHua avatar May 13 '24 06:05 ZiRunHua

If you want to use the logging options in server.Options{}, you will need to call ConfigureLogger() on the server instance afterwards.

Then you shouldn't need to call SetLoggerV2() yourself.

neilalexander avatar May 13 '24 10:05 neilalexander