nats-io / nats-server

High-Performance server for NATS.io, the cloud and edge native messaging system.
https://nats.io
Apache License 2.0
15.99k stars 1.41k forks source link

Embedded nats servers with opt.LogFile have no logging #5411

Open ZiRunHua opened 6 months ago

ZiRunHua commented 6 months ago

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 commented 6 months ago

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]
neilalexander commented 6 months ago

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.