-
Notifications
You must be signed in to change notification settings - Fork 4.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding minute_file_sink_mt #2727 #2729
base: v1.x
Are you sure you want to change the base?
Conversation
example driver code: auto logger = spdlog::minute_logger_mt("minutes_basic_logger", "logs/min-log.txt",false,60);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I commented on the points of concern.
It must also pass @gabime review after modification.
{ | ||
filename_t basename, ext; | ||
std::tie(basename, ext) = details::file_helper::split_by_extension(filename); | ||
return fmt_lib::format(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}-{:02d}_{:02d}{}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return fmt_lib::format(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}-{:02d}_{:02d}{}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, | |
return fmt_lib::format(SPDLOG_FMT_STRING(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}-{:02d}_{:02d}{}")), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated, under testing
filename_t basename, ext; | ||
std::tie(basename, ext) = details::file_helper::split_by_extension(filename); | ||
return fmt_lib::format(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}-{:02d}_{:02d}{}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, | ||
now_tm.tm_mday, now_tm.tm_hour,now_tm.tm_min,ext);//colock details define in os-inl.h |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now_tm.tm_mday, now_tm.tm_hour,now_tm.tm_min,ext);//colock details define in os-inl.h | |
now_tm.tm_mday, now_tm.tm_hour,now_tm.tm_min,ext); |
class minute_file_sink final : public base_sink<Mutex> | ||
{ | ||
public: | ||
// create hourly file sink which rotates on given time |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// create hourly file sink which rotates on given time | |
// create every minute file sink which rotates on given time |
bool should_rotate = time >= rotation_tp_; | ||
if (should_rotate) | ||
{ | ||
if (remove_init_file_) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why you would delete an empty log file.
Why is this necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part kept as it is from hourly log sink.
bool should_rotate = time >= rotation_tp_; if (should_rotate) { if (remove_init_file_) { file_helper_.close(); details::os::remove(file_helper_.filename());
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somehow I figured out why this code is there.
Perhaps, the author of sink wanted to clean up the log file created in the constructor, if it was empty, removing it at the time of rotation.
The following scenario is assumed.
- Create log file
log.2023-01-01-12-01.txt
in the constructor. - More than one minute passes without a log being recorded.
- The contents of the file to be rotated are empty. Delete it as unnecessary and record log messages in a new log file.
Can you add a comment to this code as I am having difficulty understanding the purpose of this code?
* If max_files > 0, retain only the last max_files and delete previous. | ||
*/ | ||
template<typename Mutex, typename FileNameCalc = minute_filename_calculator> | ||
class minute_file_sink final : public base_sink<Mutex> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
daily_sink and hourly_sink can change the rotation time, but minute_file_sink
seems to have a fixed rotation time. I suggest renaming it to every_minute_file_sink
to avoid confusion.
{ | ||
auto now = log_clock::now(); | ||
tm date = now_tm(now); | ||
date.tm_min = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
date.tm_min = 0; |
The most important rotation feature seems to be broken.
The minute of the next rotation time is always 1 (1 minute every hour).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tt4g
Thanks for suggestions and review,I will add configuration to make X MIN interval to do the rotation as configurable number of minutes.Hope it more flexible.I also go through other changes as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated with configurable rotation interval minutes
auto logger = spdlog::minute_logger_mt("basic_logger", "logs/min-log.txt",false,60,2);
Fixes as per suggestions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed suggested changes and configurable minutes
I think the current rotation implementation is wrong. |
Only sink name change required? hope the other implemented logic and suggestions are fixed? |
Did you not develop this sink because you wanted it? |
|
||
if (rotation_minute < 0 || rotation_minute > 59) | ||
{ | ||
throw_spdlog_ex("daily_file_sink: Invalid rotation time in ctor"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Why can't it be greater than 59? what would happen if someone wants 90 minutes for example?
- should be "minute_file_sink: Invalid rotation_minutes value in ctor"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback and got your point, remove the validation considering the concern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mmanoj Note: In the current rotation implementation, rotation_m_
should not be allowed to be 0
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tt4g
Implement the check for 0 min.
Yes, This is required as per my project requirements and hope many people like the feature.I change the name to "minute_interval_logger_mt" to make it meaningful. |
Change the logger name to reflect the minutes interval based rotation and remove the check for max min 59 in configuration.
@mmanoj Please add a unit test for this sink. Something very basic just to verify it compiles right (and perhaps test to validate it throws on invalid input). |
|
||
template<typename Factory = spdlog::synchronous_factory> | ||
inline std::shared_ptr<logger> minute_interval_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate = false, | ||
uint16_t max_files = 0,int minute = 0, const file_event_handlers &event_handlers = {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uint16_t max_files = 0,int minute = 0, const file_event_handlers &event_handlers = {}) | |
uint16_t max_files = 0, int minute = 1, const file_event_handlers &event_handlers = {}) |
minute = 0
will thrown.
// | ||
template<typename Factory = spdlog::synchronous_factory> | ||
inline std::shared_ptr<logger> minute_interval_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate = false, | ||
uint16_t max_files = 0, int minute = 0, const file_event_handlers &event_handlers = {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uint16_t max_files = 0, int minute = 0, const file_event_handlers &event_handlers = {}) | |
uint16_t max_files = 0, int minute = 1, const file_event_handlers &event_handlers = {}) |
minute = 0
will thrown.
|
|
Updated as per advice.Please check and update if any changes required.
However, for this to work the generated filename should probably also contain seconds in the name and the units restricted to seconds or higher,(e.g. not accept milliseconds or micros using
|
using std::chrono::duration
Updated as per advice.Please check and update.Once this clear I will work on the unit test |
I see the pr contains 2 sink files? which one is it? |
https://github.com/mmanoj/spdlog/blob/v1.x/include/spdlog/sinks/periodic_file_sink.h |
Any feedback regarding the PR? |
@@ -0,0 +1,217 @@ | |||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this file as we use the other one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
// create every periodic file sink which rotates on given time | ||
periodic_file_sink( | ||
filename_t base_filename, bool truncate = false, uint16_t max_files = 0, | ||
std::chrono::duration<int> rotation_period = 0, const file_event_handlers &event_handlers = {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be
periodic_file_sink(
filename_t base_filename,
std::chrono::minutes rotation_interval,
uint16_t max_files max_files,
bool truncate_first = false,
const file_event_handlers &event_handlers = {})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
{ | ||
throw_spdlog_ex("periodic_file_sink: Invalid rotation time in ctor"); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should add some sanity restriction on max_files, especially since we store all the names in a buffer
if (max_files > 2000)
{
throw_spdlog_ex("periodic_file_sink: max_files param exceeds max of 2000");
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Thanks. Would you add a test? |
If current implementation is fine,I will work on it.Please confirm. |
The thing is, I cannot know if it even complies without a minimal test. Just add minimal test that includes it and use it in the most basic way. |
sure, will work on this and update you. |
Can you help to understand how spdlog test framework work.I'm not able to run the test cases except example application under spdlog/tests/example directory. for example how to run test_daily_logger.cpp |
inline std::shared_ptr<logger> periodic_logger_mt(const std::string &logger_name, const filename_t &filename,std::chrono::minutes period = 1,uint16_t max_files = 0, bool truncate = false, | ||
const file_event_handlers &event_handlers = {}) | ||
{ | ||
return Factory::template create<sinks::periodic_file_sink_mt>(logger_name, filename,period,max_files, truncate, event_handlers); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return Factory::template create<sinks::periodic_file_sink_mt>(logger_name, filename,period,max_files, truncate, event_handlers); | |
return Factory::template create<sinks::periodic_file_sink_mt>(logger_name, filename, period,max_files, truncate, event_handlers); |
inline std::shared_ptr<logger> periodic_logger_st(const std::string &logger_name, const filename_t &filename,std::chrono::minutes period = 1,uint16_t max_files = 0, bool truncate = false, | ||
const file_event_handlers &event_handlers = {}) | ||
{ | ||
return Factory::template create<sinks::periodic_file_sink_st>(logger_name, filename,period, max_files,truncate, event_handlers); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return Factory::template create<sinks::periodic_file_sink_st>(logger_name, filename,period, max_files,truncate, event_handlers); | |
return Factory::template create<sinks::periodic_file_sink_st>(logger_name, filename, period, max_files,truncate, event_handlers); |
: base_filename_(std::move(base_filename)) | ||
, file_helper_{event_handlers} | ||
, truncate_(truncate_first) | ||
, max_files_(max_files) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great if the max file size was also configurable
like 20 * 1024 *1024 (20MB)
like rotating_file_sink
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion,I will look in to that.I have some issues on running the test case as I mention above.If someone can help to fix it I can quickly close the PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't help, unfortunately I'm not Not familiar with the test case either
Catch2 tutorial may help
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
by the way, to many commits in your pr. should have rebase first
the title of pr also should change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay due to some urgent tasks,I'm going to start working on this again.Will update the progress
Adding minute_file_sink_mt #2727 to support minute_file_sink to support every 1 min file rotation.
minute_file_sink.h.txt
5306b1f