-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.R
56 lines (46 loc) · 1.16 KB
/
logger.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
FATAL <- 1L
names(FATAL) <- "FATAL"
ERROR <- 2L
names(ERROR) <- "ERROR"
WARN <- 4L
names(WARN) <- "WARN"
INFO <- 6L
names(INFO) <- "INFO"
DEBUG <- 8L
names(DEBUG) <- "DEBUG"
TRACE <- 9L
names(TRACE) <- "TRACE"
logger.layout <- function(level, msg, id='', ...) {
the.time <- format(Sys.time(), "%Y-%m-%d %H:%M:%S")
if (length(list(...)) > 0) {
parsed <- lapply(list(...), function(x) if(is.null(x)) 'NULL' else x )
msg <- do.call(sprintf, c(msg, parsed))
}
sprintf("[%s] %s\n", names(level), msg)
}
logger.log_level <- function(msg, ..., level)
{
if (level <= logger.threshold) {
message <- logger.layout(level, msg, name, ...)
cat(message)
}
}
logger.trace <- function(msg, ...) {
logger.log_level(msg, ..., level=TRACE)
}
logger.debug <- function(msg, ...) {
logger.log_level(msg, ..., level=DEBUG)
}
logger.info <- function(msg, ...) {
logger.log_level(msg, ..., level=INFO)
}
logger.warn <- function(msg, ...) {
logger.log_level(msg, ..., level=WARN)
}
logger.error <- function(msg, ...) {
logger.log_level(msg, ..., level=ERROR)
}
logger.fatal <- function(msg, ...) {
logger.log_level(msg, ..., level=FATAL)
}
logger.threshold = TRACE