-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwriteErrorLog.R
89 lines (74 loc) · 1.85 KB
/
writeErrorLog.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
library(xlsx)
library(lubridate)
library(tidyverse)
#' Inserted into a TryCatch to write a function to an Error log
#'
#' @error An error generated by tryCatch
#' @pipeline A pipelinename, presumably provided in a config file
#' @writeLocation A folder location to write errors to.
#' @returns an excel spreadsheet of errors. This spreadsheet is either
#' created at the time the error is collected, or appended to
#' and existing sheet. Rows:
#' timestamp: Timestamp for error -- written in UTC
#' error: Error code returned from tryCatch
#' pipeline: Pipeline name.
writeErrorDataFrame <- function(
error
, pipeline
, writeLocation
){
# write initial data
data <- list(
timestamp = c(lubridate::now(tzone = 'UTC') %>% format('%Y.%m.%d %H:%M:%S'))
, error = c(error)
, pipeline = c(pipeline)
)
# Write to Dataframe
df <- do.call(
cbind
, data
) %>%
as.data.frame %>%
mutate_all(as.character)
# Generate log File name:
fileName_log <- paste(
writeLocation
, '/errorLog.xlsx'
, sep = ''
)
# Check for excel file:
if(file.exists(fileName_log)){
# if file name does exits, read file and concatenate
logFile <- xlsx::read.xlsx(
fileName_log
, sheetName = "ErrorSheets"
) %>%
rbind(
df
)
# Rewrite file.
xlsx::write.xlsx(
logFile
, paste(
writeLocation
, '/errorLog.xlsx'
, sep = ''
)
, row.names = FALSE
, sheetName = "ErrorSheets"
)
} else {
# if file doesn't exist, write to file
xlsx::write.xlsx(
df
, paste(
writeLocation
, '/errorLog.xlsx'
, sep = ''
)
, row.names = FALSE
, sheetName = "ErrorSheets"
)
}
print("log file written")
}