main.cpp
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
#include <iostream>
int main()
{
// logger
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
if (!console_sink)
{
std::cout << "create console log error" << std::endl;
exit(0);
}
console_sink->set_level(spdlog::level::debug);
console_sink->set_pattern("[multi_sink_example] [%^%l%$] %v");
auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
"logs/errors.txt", 1024 * 1024 * 10, 3);
if (!rotating_sink) {
std::cout << "create file log error" << std::endl;
exit(0);
}
rotating_sink->set_level(spdlog::level::err);
std::vector<spdlog::sink_ptr> sinks{console_sink, rotating_sink};
auto logger =
std::make_shared<spdlog::logger>("logger", sinks.begin(), sinks.end());
logger->set_level(spdlog::level::debug);
logger->set_level(spdlog::level::info);
spdlog::register_logger(logger);
}
makefile
BINOBJ := ./logger_test
CC = g++
COMPILE_FLAG = -L. -fPIC -std=c++11
CFLAGS = $(COMPILE_FLAG) -fPIC -Wall -pipe -I.
OBJS = main.o
#-----------------------------------------------------------------------
#-----------------------------------------------------------------------
SRCS = $(OBJS:.o=.cpp)
Test : $(OBJS)
$(CC) -o $(BINOBJ) $(OBJS) $(COMPILE_FLAG)
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
%.o: %.cpp
$(CC) $(CFLAGS) -c -o $@ $<
clean:
rm $(OBJS)
rm $(BINOBJ)