Because my server may run for a long time, the log file will be too large.Is there any way to cut logs according to size or time?
-
```mkdir -p log && go run main.go -log_dir=log``` and it will be auto split in dir log – Para Jun 07 '21 at 10:31
-
It's amazing! How does it split the log file, size ? – CharmCcc Jun 07 '21 at 10:47
-
@Para I test to add 350,000 lines of logs, but it can't be split (Is it not enough?) – CharmCcc Jun 10 '21 at 10:19
2 Answers
Since you worry about a large log file, try conditional logging or occassional logging You can use the following macros to perform conditional logging:
LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
The "Got lots of cookies" message is logged only when the variable num_cookies exceeds 10. If a line of code is executed many times, it may be useful to only log a message at certain intervals. This kind of logging is most useful for informational messages.
LOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";
The above line outputs a log messages on the 1st, 11th, 21st, ... times it is executed. Note that the special google::COUNTER value is used to identify which repetition is happening.
You can combine conditional and occasional logging with the following macro.
LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << google::COUNTER
<< "th big cookie";
Instead of outputting a message every nth time, you can also limit the output to the first n occurrences:
LOG_FIRST_N(INFO, 20) << "Got the " << google::COUNTER << "th cookie";
Outputs log messages for the first 20 times it is executed. Again, the google::COUNTER identifier indicates which repetition is happening.
You can check here for more info

- 459
- 3
- 17
-
Thank you for your long answer, But the actual business scenario is to record all logs. There may be tens of thousands of logs a day. – CharmCcc Jun 07 '21 at 12:33
Now I found a way to split logs.
Using third party libraries.(ex:https://github.com/natefinch/lumberjack
)

- 113
- 11
-
The easiest way is to modify the fields `MaxSize` of the file `glog_file.go` – CharmCcc Jun 15 '21 at 07:21