Skip to content
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

How to add customized suffix append to daily file? Like "20210905.mylog" #2091

Closed
WorstCodeWay opened this issue Sep 5, 2021 · 5 comments
Closed

Comments

@WorstCodeWay
Copy link

The tile describes my demand. Thanks

@WorstCodeWay WorstCodeWay changed the title How to add customized suffixe append to daily file? Like "20210905.mylog" How to add customized suffix append to daily file? Like "20210905.mylog" Sep 5, 2021
@tt4g
Copy link
Contributor

tt4g commented Sep 5, 2021

You can use spdlog::sinks::daily_file_format_sink_mt.
e.g. std::shared_ptr<spdlog::logger> logger = spdlog::daily_logger_format_mt("daily_logger", "%Y%m%d.mylog");
See this PR #1847.

@WorstCodeWay
Copy link
Author

WorstCodeWay commented Sep 5, 2021

You can use spdlog::sinks::daily_file_format_sink_mt.

Thanks,I believe this can solve my title question, but can I further add sinks(say, qtwidget sink) into this "format daily logger")?

@tt4g
Copy link
Contributor

tt4g commented Sep 5, 2021

spdlog::sinks::dist_sink_mt can combine multiple sinks into a single sink.

auto dist_sink = std::make_shared<spdlog::sinks::dist_sink_mt>();

dist_sink->add_sink(
    std::make_shared<spdlog::sinks::daily_file_format_sink_mt>(
        "%Y%m%d.mylog", ...(other args) ));

// Add other sinks.
dist_sink->add_sink( ... );

auto dist_logger = std::make_shared<spdlog::logger>("dist_logger", dist_sink);
spdlog::register_logger(dist_logger);

@WorstCodeWay
Copy link
Author

spdlog::sinks::dist_sink_mt can combine multiple sinks into a single sink.

auto dist_sink = std::make_shared<spdlog::sinks::dist_sink_mt>();

dist_sink->add_sink(
    std::make_shared<spdlog::sinks::daily_file_format_sink_mt>(
        "%Y%m%d.mylog", ...(other args) ));

// Add other sinks.
dist_sink->add_sink( ... );

auto dist_logger = std::make_shared<spdlog::logger>("dist_logger", dist_sink);
spdlog::register_logger(dist_logger);

Nice!I will try it.

@WorstCodeWay
Copy link
Author

spdlog::sinks::dist_sink_mt can combine multiple sinks into a single sink.

auto dist_sink = std::make_shared<spdlog::sinks::dist_sink_mt>();

dist_sink->add_sink(
    std::make_shared<spdlog::sinks::daily_file_format_sink_mt>(
        "%Y%m%d.mylog", ...(other args) ));

// Add other sinks.
dist_sink->add_sink( ... );

auto dist_logger = std::make_shared<spdlog::logger>("dist_logger", dist_sink);
spdlog::register_logger(dist_logger);

I have tried, and just daily_file_format_sink_mt satisfies my demand: add a daily file sink with custom suffix append onto log file, like ".mylog", and add this sink into logger which will add further sinks, so codes is:

auto formatfilesink = std::make_shared<spdlog::sinks::daily_file_format_sink_mt>("Log_%Y-%m-%d.log", 23, 59);
auto logger = std::make_shared<spdlog::logger>("LogName",  _sysFileSink);

logger->sinks().push_back(/*some other sink*/);

That works just fine.

Thank you again, @tt4g.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants