Skip to content

Commit 782e791

Browse files
committed
Enhance logger API: more levels (debug, trace, error, critical), add logger_from_config, update quickstart example, add logger_debug.hpp helper
1 parent 3c41837 commit 782e791

3 files changed

Lines changed: 47 additions & 5 deletions

File tree

examples/quickstart.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
#include "../include/logger.hpp"
2+
#include "../include/logger_config.hpp"
3+
#include <memory>
24

35
int main() {
4-
Logger log; // async by default
5-
log.info("app.start").kv("version", "0.1").send();
6-
log.error("db.fail").kv("query", "SELECT * FROM foo").kv("code", 10).send();
7-
log.set_level(Logger::Level::Warning);
8-
log.warn("low.battery").kv("percent", 15).send();
6+
// Load config from file if exists, fallback to default async logger
7+
auto log = c_log::logger_from_config("logger.json");
8+
log->info("app.start").kv("version", "0.1");
9+
log->error("db.fail").kv("query", "SELECT * FROM foo").kv("code", 10);
10+
log->set_level(c_log::Level::Warning);
11+
log->warn("low.battery").kv("percent", 15);
12+
// manual flush happens automatically at destruction
13+
return 0;
914
}

include/logger.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,32 @@ class Logger {
6565
cur_entry_ = Entry{event, Level::Info};
6666
return *this;
6767
}
68+
Logger& debug(const std::string& event) {
69+
flush_if_building();
70+
cur_entry_ = Entry{event, Level::Debug};
71+
return *this;
72+
}
73+
Logger& warning(const std::string& event) {
74+
flush_if_building();
75+
cur_entry_ = Entry{event, Level::Warning};
76+
return *this;
77+
}
78+
Logger& warn(const std::string& event) { return warning(event); }
79+
Logger& error(const std::string& event) {
80+
flush_if_building();
81+
cur_entry_ = Entry{event, Level::Error};
82+
return *this;
83+
}
84+
Logger& trace(const std::string& event) {
85+
flush_if_building();
86+
cur_entry_ = Entry{event, Level::Trace};
87+
return *this;
88+
}
89+
Logger& critical(const std::string& event) {
90+
flush_if_building();
91+
cur_entry_ = Entry{event, Level::Critical};
92+
return *this;
93+
}
6894
Logger& kv(const std::string& key, const std::string& val) {
6995
cur_entry_.fields.emplace_back(key, val);
7096
return *this;

include/logger_debug.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
#include <string>
3+
#include <iostream>
4+
namespace c_log {
5+
// Debug helper (quick log to stderr, compile-out in release if defined)
6+
#ifndef CLOG_DISABLE_DEBUG
7+
inline void debug_log(const std::string& msg) { std::cerr << "[cLog-debug] " << msg << std::endl; }
8+
#else
9+
inline void debug_log(const std::string&) {}
10+
#endif
11+
}

0 commit comments

Comments
 (0)