Skip to content

Instantly share code, notes, and snippets.

@atalv
Last active September 23, 2022 14:26
Show Gist options
  • Save atalv/f4a4099d6e9fab55081575c3b50828f3 to your computer and use it in GitHub Desktop.
Save atalv/f4a4099d6e9fab55081575c3b50828f3 to your computer and use it in GitHub Desktop.
Logging related functions using R package 'logger', and some reference utility functions being used from the logger_utils.R gist (https://gist.github.com/atalv/f98b928c870630c677694b5edac072dc)
require(logger)
require(utils)
#' Logging related functions
#'
#' Logger function defined which are created on top of
#' \code{\link[logger]{log_level}} -
#' which is part of another package \code{logger}. Additional
#' capabilities have been added to those functions which enables this function
#' to be able to send logs directly to the Azure log analytics workspace, and
#' also have control to post log outputs into the console - as per user input.
#' Note that, logging threshold can be directly set (if needed) using
#' \code{\link[logger]{log_threshold}} function from \code{logger} package.
#'
#' @param ... Content(s) of this argument is directly passed on to
#' \code{\link[logger]{log_level}} function of the \code{logger}
#' package.
#' @param logtoazure If \code{TRUE} then logs will be sent to Azure log
#' analytics workspace and console. Else if \code{FALSE} then log will not
#' be sent to Azure log analytics workspace, it will only be displayed on
#' console, which is the default layout of \code{logger} package.
#' @param log_type Single element character vector is expected. Logs will be
#' posted to this event on Azure log analytics. For details, check this:
#' \url{https://docs.microsoft.com/en-us/azure/azure-monitor/logs/data-collector-api}
#' @param log_customer_id Workspace ID of Azure log analytics workspace.
#' @param log_shared_key Shared key of Azure log analytics workspace.
#'
#' @return If \code{logtoazure} is \code{FALSE} then log output is shown on
#' console. Else, if \code{TRUE}, then log output is shown on console, as
#' well as posted to Azure log analytics workspace under the custom table
#' name as specified by \code{log_type} argument. If POST request is
#' unsuccessful, then additional warning message is thrown with POST request
#' response.
#' @note Logging layout is set in JSON format, required to send to AZ log
#' analytics. Note that this layout modifies the global namespace of logger
#' package by default
#' @importFrom logger log_layout log_level layout_simple layout_json INFO
#' @importFrom utils capture.output
#' @rdname logging
#' @aliases logger_level logger_info logger_error logger_warn
#' @examples
#' \dontrun{
#' logger_level(logger::INFO, "logging message", logtoazure = FALSE)
#' }
#'
logger_level <- function(...,
logtoazure = FALSE,
log_type = "custom_log",
log_customer_id = "xxxx",
log_shared_key = "yyyy") {
logger::log_layout(
logger::layout_json(c("time", "level", "msg"))
)
logger::log_level(...)
body <- capture.output(logger::log_level(...), type = "message")
if (logtoazure) {
response <- try(.post_data(customer_id = log_customer_id,
shared_key = log_shared_key,
body, log_type), silent = TRUE)
if (base::inherits(response, "try-error")) {
warning(paste0("Some error happened while sending POST request ",
"to AZ log analytics workspace. Error message: ",
as.character(response)))
} else if (response$status_code >= 200 & response$status_code <= 299) {
invisible(response)
} else {
warning(paste0("Could not post to AZ log analytics, status code: ",
response$status_code, ".", "\n",
"Response received: ",
httr::content(response, as = "text")))
}
}
}
#'
#' @rdname logging
#' @aliases logger_info
#' @note \code{logger_info} is a wrapper function around
#' \code{\link{logger_level}} - logging level is set as
#' \code{\link[logger]{INFO}} by default.
#'
#' @export
#' @importFrom logger INFO
#' @examples
#' \dontrun{
#' logger_info("logging message info", logtoazure = FALSE)
#' }
#'
logger_info <- function(...) {
logger_level(logger::INFO, ...)
}
#'
#' @rdname logging
#' @aliases logger_error
#' @note \code{logger_error} is a wrapper function around
#' \code{\link{logger_level}} - logging level is set as
#' \code{\link[logger]{ERROR}} by default.
#'
#' @export
#' @importFrom logger ERROR
#' @examples
#' \dontrun{
#' logger_error("logging message error", logtoazure = FALSE)
#' }
#'
logger_error <- function(...) {
logger_level(logger::ERROR, ...)
}
#'
#' @rdname logging
#' @aliases logger_warn
#' @note \code{logger_warn} is a wrapper function around
#' \code{\link{logger_level}} - logging level is set as
#' \code{\link[logger]{WARN}} by default.
#'
#' @export
#' @importFrom logger WARN
#' @examples
#' \dontrun{
#' logger_warn("logging message warn", logtoazure = FALSE)
#' }
#'
logger_warn <- function(...) {
logger_level(logger::WARN, ...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment