Updated my error log functionality to go to an error.log file instead of to the console.

This commit is contained in:
rapturate
2026-06-09 14:57:38 -04:00
parent 791e0778e3
commit dd34fa55ec
4 changed files with 50 additions and 6 deletions

10
.env
View File

@@ -1,2 +1,8 @@
DB_PATH=/home/rapturate/Code/cpp/parselog_cli/data/GeoLite2-City.mmdb
LOG_PATH=/home/rapturate/Code/cpp/parselog_cli/test_logs/access.log.txt
# ParseLogCLI Global Environment Configuration
# Generated on 2026-06-09
# Path to the MaxMind GeoLite2 City Database binary
DB_PATH=data/GeoLite2-City.mmdb
# Absolute or relative path to the server access logs target file
LOG_PATH=test_logs/access.log.txt

2
.gitignore vendored
View File

@@ -46,5 +46,5 @@ compile_commands.json
# ==========================================
# Sensitive Files
# ==========================================
.env
*.env
.env

View File

@@ -1,9 +1,12 @@
#include <exception>
#include <iostream>
#include <cstdlib>
#include <map>
#include <string>
#include <fstream>
#include <filesystem>
#include <chrono>
#include <iomanip>
#include "ip_to_geo.hpp"
@@ -13,6 +16,20 @@ loc_data iplookup(const std::string& ip) {
const char* env_db_path = std::getenv("DB_PATH");
std::string db_path = (env_db_path != nullptr) ? env_db_path : "data/GeoLite2-City.mmdb";
// Verify if the database file physically exists on the disk
if (!std::filesystem::exists(db_path)) {
std::ofstream error_file("error.log", std::ios::app);
if (error_file.is_open()) {
auto now = std::chrono::system_clock::now();
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
error_file << "[" << std::put_time(std::localtime(&now_time), "%Y-%m-%d %H:%M:%S") << "] "
<< "Error: Database file missing at path: " << db_path << " during IP lookup for: " << ip << std::endl;
error_file.close();
}
return loc_data{};
}
loc_data location;
@@ -26,7 +43,17 @@ loc_data iplookup(const std::string& ip) {
location.longitude = geo_data["longitude"];
}
catch (const std::exception& e) {
//std::cerr << "Database failed to load: " << e.what() << std::endl;
std::ofstream error_file("error.log", std::ios::app);
if (error_file.is_open()) {
auto now = std::chrono::system_clock::now();
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
error_file << "[" << std::put_time(std::localtime(&now_time), "%Y-%m-%d %H:%M:%S") << "] "
<< "Database failed to load: " << e.what() << std::endl;
error_file.close();
}
}
return location;
};

View File

@@ -2,6 +2,8 @@
#include <iostream>
#include <fstream>
#include <ostream>
#include <chrono>
#include <iomanip>
#include <string>
#include <vector>
#include <regex>
@@ -15,7 +17,16 @@ p_logs::p_logs(std::string log_path) {
}
std::ifstream file(log_path);
if (!file.is_open()) {
std::cerr << "Error loading " << log_path << std::endl;
std::ofstream error_file("error.log", std::ios::app);
if (error_file.is_open()) {
auto now = std::chrono::system_clock::now();
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
error_file << "[" << std::put_time(std::localtime(&now_time), "%Y-%m-%d %H:%M:%S") << "] "
<< "Log file failed to load at: " << log_path << std::endl;
error_file.close();
}
return;
}