Skip to content

Commit

Permalink
Funksjon for å aktivere logging i json-format
Browse files Browse the repository at this point in the history
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
  • Loading branch information
arnfinn committed Nov 22, 2024
1 parent 5bef250 commit 5c63fe4
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 40 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Imports:
jsonlite,
kableExtra,
knitr,
logger,
magrittr,
readr,
rlang,
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export(loadRegData)
export(loadStagingData)
export(logFormat)
export(logTimeFrame)
export(loggerSetup)
export(makeAutoReportTab)
export(makeRunDayOfYearSequence)
export(mst)
Expand Down
54 changes: 39 additions & 15 deletions R/appLog.R
Original file line number Diff line number Diff line change
@@ -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()

Check warning on line 37 in R/appLog.R

View check run for this annotation

Codecov / codecov/patch

R/appLog.R#L36-L37

Added lines #L36 - L37 were not covered by tests
}
}
15 changes: 15 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ reference:
desc: >
Handle logging
contents:
- loggerSetup
- logger
- sanitizeLog

Expand Down
2 changes: 1 addition & 1 deletion man/appLog.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/loggerSetup.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 0 additions & 19 deletions man/rapbase.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 40 additions & 4 deletions tests/testthat/test-log.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -225,3 +225,39 @@ 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"
)$default$record

# Test what has been logged
expect_true(jsonlite::validate(infoLogjson))
infoLog <- jsonlite::fromJSON(infoLogjson)
expect_equal(nchar(infoLog$time), 23)
expect_equal(infoLog$level, "INFO")
expect_equal(infoLog$message, "Test log setup")
expect_equal(infoLog$app, "rapbasis")
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)
}

expect_error(loggerSetup())
})
Binary file added tests/testthat/tmp.rds
Binary file not shown.

0 comments on commit 5c63fe4

Please sign in to comment.