Created
September 14, 2024 22:44
-
-
Save xbalaji/ed79555b7e85e95d69a6dbba18a50611 to your computer and use it in GitHub Desktop.
customlog.R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set CRAN mirror | |
options(repos = c(CRAN = "https://cloud.r-project.org")) | |
# Install optparse if not already installed | |
if (!requireNamespace("optparse", quietly = TRUE)) { | |
install.packages("optparse") | |
} | |
library(optparse) | |
# Define command-line options | |
option_list <- list( | |
make_option(c("-l", "--log-level"), type="character", default="INFO", | |
help="Set the log level (DEBUG, INFO, WARNING, ERROR) [default=%default]"), | |
make_option(c("-f", "--log-file"), type="character", default="app.log", | |
help="Set the log file name [default=%default]") | |
) | |
# Parse command-line arguments | |
opt_parser <- OptionParser(option_list=option_list) | |
opt <- parse_args(opt_parser) | |
# Set the global log level | |
GLOBAL_LOG_LEVEL <- toupper(opt$log_level) | |
LOG_FILE <- opt$log_file | |
# Define log levels and their priorities | |
LOG_LEVELS <- c("DEBUG" = 1, "INFO" = 2, "WARNING" = 3, "ERROR" = 4) | |
# Function to get the calling script's filename | |
get_calling_filename <- function() { | |
calls <- sys.calls() | |
if (length(calls) >= 3) { | |
call <- calls[[length(calls) - 2]] | |
if (is.call(call) && identical(call[[1]], as.name("source"))) { | |
return(basename(as.character(call[[2]]))) | |
} | |
} | |
return(basename(sys.frame(1)$ofile)) | |
} | |
# Custom Logging Function | |
custom_log <- function(level, message) { | |
if (LOG_LEVELS[level] >= LOG_LEVELS[GLOBAL_LOG_LEVEL]) { | |
# Get current timestamp | |
timestamp <- format(Sys.time(), "%Y-%m-%d %I:%M:%S %p") | |
# Get the filename of the calling script | |
filename <- get_calling_filename() | |
# Create log entry | |
log_entry <- sprintf("%s:%s:%s: %s\n", filename, level, timestamp, message) | |
# Append log entry to file | |
cat(log_entry, file = LOG_FILE, append = TRUE) | |
# Print log entry to console | |
cat(log_entry) | |
} | |
} | |
# Logging functions for different levels | |
log_debug <- function(message) { | |
custom_log("DEBUG", message) | |
} | |
log_info <- function(message) { | |
custom_log("INFO", message) | |
} | |
log_warning <- function(message) { | |
custom_log("WARNING", message) | |
} | |
log_error <- function(message) { | |
custom_log("ERROR", message) | |
} | |
# Test the logging functions | |
log_debug("This is a debug message") | |
log_info("Application started") | |
log_warning("Missing data in row 10") | |
log_error("Failed to connect to server") | |
# Print the current log level | |
cat(sprintf("Current log level: %s\n", GLOBAL_LOG_LEVEL)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment