From 937b1b6f05beb4ae8458167bbe1da1dcd97bfbf8 Mon Sep 17 00:00:00 2001 From: arnfinn Date: Fri, 22 Nov 2024 10:58:50 +0100 Subject: [PATCH] =?UTF-8?q?Funksjon=20for=20=C3=A5=20aktivere=20logging=20?= =?UTF-8?q?i=20json-format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit La det inn i fila appLog.R. Det som lå der var en definisjon noe data i pakken. Denne definisjonen flyttet jeg til data.R --- DESCRIPTION | 3 ++- NAMESPACE | 1 + R/appLog.R | 54 +++++++++++++++++++++++++++----------- R/data.R | 15 +++++++++++ _pkgdown.yml | 1 + man/appLog.Rd | 2 +- man/loggerSetup.Rd | 23 ++++++++++++++++ man/rapbase.Rd | 19 -------------- tests/testthat/test-log.R | 43 +++++++++++++++++++++++++++--- tests/testthat/tmp.rds | Bin 0 -> 4791 bytes 10 files changed, 121 insertions(+), 40 deletions(-) create mode 100644 R/data.R create mode 100644 man/loggerSetup.Rd create mode 100644 tests/testthat/tmp.rds diff --git a/DESCRIPTION b/DESCRIPTION index 40d511b2..39a465ee 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -38,6 +38,7 @@ Imports: jsonlite, kableExtra, knitr, + logger, magrittr, readr, rlang, @@ -50,7 +51,7 @@ Imports: sship (>= 0.9.0), utils, yaml -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 URL: https://github.com/Rapporteket/rapbase BugReports: https://github.com/Rapporteket/rapbase/issues Suggests: diff --git a/NAMESPACE b/NAMESPACE index e89b7b54..d99fa1a3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -51,6 +51,7 @@ export(loadRegData) export(loadStagingData) export(logFormat) export(logTimeFrame) +export(loggerSetup) export(makeAutoReportTab) export(makeRunDayOfYearSequence) export(mst) diff --git a/R/appLog.R b/R/appLog.R index 46c971e6..be98086a 100644 --- a/R/appLog.R +++ b/R/appLog.R @@ -1,15 +1,39 @@ -#' App log test dataset. -#' -#' A dataset containing test entries for the application log. -#' -#' @format A data frame with 20 rows and 7 variables: -#' \describe{ -#' \item{time}{character timestamp} -#' \item{user}{user name} -#' \item{name}{user full name} -#' \item{group}{users group/registry} -#' \item{role}{users role} -#' \item{resh_id}{users organization} -#' \item{message}{log message} -#' } -"appLog" +#' Settings for logging as json +#' +#' Every info, warning and error will be logged in json format. +#' +#' @param usernameEnv Global variable containing user name +#' @param appidEnv Global variable containing application name +#' @param testing Is the if function running in a test? +#' Function will skip some calls if it does. +#' +#' @export +#' +loggerSetup <- function( + usernameEnv = "SHINYPROXY_USERNAME", + appidEnv = "SHINYPROXY_APPID", + testing = FALSE +) { + logger::log_threshold(logger::INFO) + formatterJson <- function(level, message, ...) { + username <- Sys.getenv(usernameEnv, unset = "unknown") + appid <- Sys.getenv(appidEnv, unset = "unknown") + return(jsonlite::toJSON( + list( + time = format(Sys.time(), "%Y-%m-%d %H:%M:%OS3"), + level = attr(level, "level"), + app = appid, + user = username, + message = message + ), + auto_unbox = TRUE + ) + ) + } + logger::log_layout(formatterJson) + if (!testing) { + logger::log_messages() + logger::log_warnings() + logger::log_errors() + } +} diff --git a/R/data.R b/R/data.R new file mode 100644 index 00000000..46c971e6 --- /dev/null +++ b/R/data.R @@ -0,0 +1,15 @@ +#' App log test dataset. +#' +#' A dataset containing test entries for the application log. +#' +#' @format A data frame with 20 rows and 7 variables: +#' \describe{ +#' \item{time}{character timestamp} +#' \item{user}{user name} +#' \item{name}{user full name} +#' \item{group}{users group/registry} +#' \item{role}{users role} +#' \item{resh_id}{users organization} +#' \item{message}{log message} +#' } +"appLog" diff --git a/_pkgdown.yml b/_pkgdown.yml index 8e113148..8742354c 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -51,6 +51,7 @@ reference: desc: > Handle logging contents: + - loggerSetup - logger - sanitizeLog diff --git a/man/appLog.Rd b/man/appLog.Rd index 7982cb0c..614dd949 100644 --- a/man/appLog.Rd +++ b/man/appLog.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/appLog.R +% Please edit documentation in R/data.R \docType{data} \name{appLog} \alias{appLog} diff --git a/man/loggerSetup.Rd b/man/loggerSetup.Rd new file mode 100644 index 00000000..fe21c083 --- /dev/null +++ b/man/loggerSetup.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/appLog.R +\name{loggerSetup} +\alias{loggerSetup} +\title{Settings for logging as json} +\usage{ +loggerSetup( + usernameEnv = "SHINYPROXY_USERNAME", + appidEnv = "SHINYPROXY_APPID", + testing = FALSE +) +} +\arguments{ +\item{usernameEnv}{Global variable containing user name} + +\item{appidEnv}{Global variable containing application name} + +\item{testing}{Is the if function running in a test? +Function will skip some calls if it does.} +} +\description{ +Every info, warning and error will be logged in json format. +} diff --git a/man/rapbase.Rd b/man/rapbase.Rd index 3c0ba8ed..03ec5216 100644 --- a/man/rapbase.Rd +++ b/man/rapbase.Rd @@ -1,7 +1,6 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/rapbase.R \name{rapbase} -\alias{rapbase-package} \alias{rapbase} \title{rapbase: Base Functions and Resources for Rapporteket} \description{ @@ -9,21 +8,3 @@ Provide common functions and resources for registry specific R-packages at Rapporteket. This packages is relevant for developers of packages/registries at Rapporteket } -\seealso{ -Useful links: -\itemize{ - \item \url{https://github.com/Rapporteket/rapbase} - \item Report bugs at \url{https://github.com/Rapporteket/rapbase/issues} -} - -} -\author{ -\strong{Maintainer}: Are Edvardsen \email{biorakel@gmail.com} (\href{https://orcid.org/0000-0002-5210-3656}{ORCID}) - -Authors: -\itemize{ - \item Kevin Otto Thon \email{kevin.thon@helse-nord.no} - \item Arnfinn Hykkerud Steindal \email{arnfinn.steindal@gmail.com} (\href{https://orcid.org/0000-0002-4088-7860}{ORCID}) -} - -} diff --git a/tests/testthat/test-log.R b/tests/testthat/test-log.R index c06582b2..f897a989 100644 --- a/tests/testthat/test-log.R +++ b/tests/testthat/test-log.R @@ -213,10 +213,10 @@ test_that("append and read errors when target is not known", { # remove test db if (is.null(check_db(is_test_that = FALSE))) { con <- RMariaDB::dbConnect(RMariaDB::MariaDB(), - host = Sys.getenv("DB_HOST"), - user = Sys.getenv("DB_USER"), - password = Sys.getenv("DB_PASS"), - bigint = "integer" + host = Sys.getenv("DB_HOST"), + user = Sys.getenv("DB_USER"), + password = Sys.getenv("DB_PASS"), + bigint = "integer" ) RMariaDB::dbExecute(con, paste("DROP DATABASE", nameLogDb)) rapbase::rapCloseDbConnection(con) @@ -225,3 +225,38 @@ if (is.null(check_db(is_test_that = FALSE))) { # Restore instance Sys.setenv(R_RAP_CONFIG_PATH = currentConfig) Sys.setenv(R_RAP_INSTANCE = currentInstance) + +test_that("loggerSetup is working", { + # env-stuff + currentUser <- Sys.getenv("SHINYPROXY_USERNAME") + currentApp <- Sys.getenv("SHINYPROXY_APPID") + Sys.setenv(SHINYPROXY_USERNAME = "jesus@sky.com") + Sys.setenv(SHINYPROXY_APPID = "rapbasis") + + # run the function we want to test + loggerSetup(testing = TRUE) + + # log something + infoLogjson <- logger::log_info( + "Test log setup", + namespace = "tmp" + )$default$record + + # Test what has been logged + testthat::expect_true(jsonlite::validate(infoLogjson)) + infoLog <- jsonlite::fromJSON(infoLogjson) + testthat::expect_equal(nchar(infoLog$time), 23) + testthat::expect_equal(infoLog$level, "INFO") + testthat::expect_equal(infoLog$message, "Test log setup") + testthat::expect_equal(infoLog$app, "rapbasis") + testthat::expect_equal(infoLog$user, "jesus@sky.com") + + # env-stuff + if (currentUser == "" && currentApp == "") { + Sys.unsetenv("SHINYPROXY_USERNAME") + Sys.unsetenv("SHINYPROXY_APPID") + } else { + Sys.setenv(SHINYPROXY_USERNAME = currentUser) + Sys.setenv(SHINYPROXY_APPID = currentApp) + } +}) diff --git a/tests/testthat/tmp.rds b/tests/testthat/tmp.rds new file mode 100644 index 0000000000000000000000000000000000000000..28f7f81fa6d7b65c97f8d6f4ca956d60f9959fac GIT binary patch literal 4791 zcmV;o5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