Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Use custom ERR logging level.

Kishore Nallan50ba0a02ceec
diff --git a/include/cmdline.h b/include/cmdline.h
index 67879084..432dc081 100644
--- a/include/cmdline.h
+++ b/include/cmdline.h
@@ -589,12 +589,12 @@ namespace cmdline{
void check(int argc, bool ok){
if ((argc==1 && !ok) || exist("help")){
- LOG(FATAL)<<usage();
+ std::cerr<<usage();
exit(0);
}
if (!ok){
- LOG(FATAL)<<error()<<std::endl<<usage();
+ std::cerr<<error()<<std::endl<<usage();
exit(1);
}
}
diff --git a/include/console_logging_sink.h b/include/console_logging_sink.h
deleted file mode 100644
index fbdbf8a7..00000000
--- a/include/console_logging_sink.h
+++ /dev/null
@@ -1,22 +0,0 @@
-#pragma once
-
-#include <string>
-#include <iostream>
-#include <g3log/logmessage.hpp>
-
-// https://github.com/KjellKod/g3sinks/blob/master/snippets/ColorCoutSink.hpp
-
-struct ConsoleLoggingSink {
- enum FG_Color {YELLOW = 33, RED = 31, GREEN=32, WHITE = 97};
-
- void ReceiveLogMessage(g3::LogMessageMover logEntry) {
- auto level = logEntry.get()._level;
-
- if (g3::internal::wasFatal(level)) {
- std::cerr << "\033[" << RED << "m"
- << logEntry.get().toString() << "\033[m";
- } else {
- std::cout << logEntry.get().toString();
- }
- }
-};
\ No newline at end of file
diff --git a/include/logger.h b/include/logger.h
index e899d493..daaaa151 100644
--- a/include/logger.h
+++ b/include/logger.h
@@ -1,5 +1,26 @@
#pragma once
+#include <string>
+#include <iostream>
#include <g3log/g3log.hpp>
#include <g3log/logworker.hpp>
-#include "console_logging_sink.h"
\ No newline at end of file
+#include <g3log/logmessage.hpp>
+
+/* Custom logging levels for use with g3log */
+const LEVELS ERR {WARNING.value + 10, {"ERROR"}};
+
+/* https://github.com/KjellKod/g3sinks/blob/master/snippets/ColorCoutSink.hpp */
+struct ConsoleLoggingSink {
+ enum FG_Color {YELLOW = 33, RED = 31, GREEN=32, WHITE = 97};
+
+ void ReceiveLogMessage(g3::LogMessageMover logEntry) {
+ auto level = logEntry.get()._level;
+
+ if (level.value >= ERR.value) {
+ std::cerr << "\033[" << RED << "m"
+ << logEntry.get().toString() << "\033[m";
+ } else {
+ std::cout << logEntry.get().toString();
+ }
+ }
+};
\ No newline at end of file
diff --git a/include/replicator.h b/include/replicator.h
index 40216844..3e58c870 100644
--- a/include/replicator.h
+++ b/include/replicator.h
@@ -117,10 +117,10 @@ public:
store._get_db_unsafe()->Write(rocksdb::WriteOptions(), &write_batch);
}
} else {
- LOG(FATAL) << "Replication error while fetching records from master, status_code=" << status_code;
+ LOG(ERR) << "Replication error while fetching records from master, status_code=" << status_code;
if(status_code != 0) {
- LOG(FATAL) << json_response;
+ LOG(ERR) << json_response;
}
}
@@ -141,8 +141,8 @@ public:
try {
collection_meta = nlohmann::json::parse(replication_event->value);
} catch(...) {
- LOG(FATAL) << "Failed to parse collection meta JSON.";
- LOG(FATAL) << "Replication event value: " << replication_event->value;
+ LOG(ERR) << "Failed to parse collection meta JSON.";
+ LOG(ERR) << "Replication event value: " << replication_event->value;
delete replication_event;
exit(1);
}
diff --git a/include/store.h b/include/store.h
index 465a6dee..3d6d3753 100644
--- a/include/store.h
+++ b/include/store.h
@@ -68,9 +68,11 @@ public:
// open DB
rocksdb::Status s = rocksdb::DB::Open(options, state_dir_path, &db);
+
if(!s.ok()) {
- LOG(FATAL) << s.ToString();
+ LOG(FATAL) << "Error while initializing store: " << s.ToString();
}
+
assert(s.ok());
}
diff --git a/src/http_server.cpp b/src/http_server.cpp
index 8512b28e..21f3dfe1 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -133,7 +133,7 @@ int HttpServer::run() {
h2o_multithread_register_receiver(message_queue, message_receiver, on_message);
if (create_listener() != 0) {
- LOG(FATAL) << "Failed to listen on " << listen_address << ":" << listen_port << " - " << strerror(errno);
+ LOG(ERR) << "Failed to listen on " << listen_address << ":" << listen_port << " - " << strerror(errno);
return 1;
} else {
LOG(INFO) << "Typesense has started. Ready to accept requests on port " << listen_port;
diff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cpp
index b82c9dbc..66a2623f 100644
--- a/src/main/typesense_server.cpp
+++ b/src/main/typesense_server.cpp
@@ -65,6 +65,8 @@ void replica_server_routes() {
}
int main(int argc, char **argv) {
+ signal(SIGINT, catch_interrupt);
+
cmdline::parser options;
options.add<std::string>("data-dir", 'd', "Directory where data will be stored.", true);
options.add<std::string>("api-key", 'k', "API key that allows all operations.", true);
@@ -87,6 +89,7 @@ int main(int argc, char **argv) {
std::string log_dir = options.get<std::string>("log-dir");
if(log_dir.empty()) {
+ // use console logger if log dir is not specified
log_worker->addSink(std2::make_unique<ConsoleLoggingSink>(),
&ConsoleLoggingSink::ReceiveLogMessage);
} else {
@@ -96,17 +99,16 @@ int main(int argc, char **argv) {
}
log_worker->addDefaultLogger("typesense", log_dir, "");
- std::cout << "Typesense has started. Log directory is configured as: " << log_dir << std::endl;
+ std::cout << "Starting Typesense " << TYPESENSE_VERSION << ". Log directory is configured as: "
+ << log_dir << std::endl;
}
g3::initializeLogging(log_worker.get());
- signal(SIGINT, catch_interrupt);
-
- LOG(INFO) << "Typesense version: " << TYPESENSE_VERSION;
+ LOG(INFO) << "Starting Typesense " << TYPESENSE_VERSION;
if(!directory_exists(options.get<std::string>("data-dir"))) {
- LOG(FATAL) << "Typesense failed to start. " << "Data directory " << options.get<std::string>("data-dir")
+ LOG(ERR) << "Typesense failed to start. " << "Data directory " << options.get<std::string>("data-dir")
<< " does not exist.";
return 1;
}
@@ -119,7 +121,7 @@ int main(int argc, char **argv) {
if(init_op.ok()) {
LOG(INFO) << "Finished loading collections from disk.";
} else {
- LOG(FATAL)<< "Typesense failed to start. " << "Could not load collections from disk: " << init_op.error();
+ LOG(ERR)<< "Typesense failed to start. " << "Could not load collections from disk: " << init_op.error();
return 1;
}
@@ -145,7 +147,7 @@ int main(int argc, char **argv) {
std::vector<std::string> parts;
StringUtils::split(master_host_port, parts, ":");
if(parts.size() != 3) {
- LOG(FATAL) << "Invalid value for --master option. Usage: http(s)://<master_address>:<master_port>";
+ LOG(ERR) << "Invalid value for --master option. Usage: http(s)://<master_address>:<master_port>";
return 1;
}