create_logger: Create a logger

Description Usage Arguments Details Value Note See Also Examples

Description

Create an object of class 'Logger' with two attributes log_time and log_message

Usage

1
create_logger(name, level, file = "")

Arguments

name

A logger name

level

A logging level. One of 'DEBUG', 'INFO', 'WARNING', 'ERROR'. Default to 'DEBUG'.

file

Either a path to a file or a connection. By default logs are printed to console.

Details

create_logger creates a reference point in logging process. If it's level argument is higher or equal to threshold_level argument set inside configure_logging, the log will be printed to console or log file, otherwise a log will not show up in the output.

NOTE that if file argument is specified, it overwrites output_file config defined inside configure_logging function.

Value

An object of class 'Logger' - list with two attributes log_time and log_message. log_time allows to track execution time in context of logger. log_message prints log message in context of logger.

Note

Both log_time and log_message are functions that can be called directly without a need to create 'logger' reference.

See Also

configure_logging, log_time, log_message

Examples

 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
# create an object of class 'Logger' with name 'clean_data' and default logging level INFO
logger <- create_logger(name = 'clean_data', level = 'INFO')

# create log in context of logger 'clean_data':
logger$log_message('Some text')
# 2016-09-21 10:59:22 - [clean_data] - INFO - [Some text]

# or track execution time in context of logger clean_data':
logger$log_time('Cleaning') %<% {
     logger$log_time('Removing NA') %<% {
         # code to remvoe NA
         Sys.sleep(0.5)
     }

     logger$log_time('Transforming') %<% {
         # code to transform data
         Sys.sleep(1)
     }
}
# 2016-10-02 23:17:16 - [clean_data] - INFO - [Start] - [Cleaning]
#  2016-10-02 23:17:16 - [clean_data] - INFO - [Start] - [Removing NA]
#  2016-10-02 23:17:16 - [clean_data] - INFO - [End  ] - [Removing NA] - [Done in 0.5 sec. (0 min.)]
#  2016-10-02 23:17:16 - [clean_data] - INFO - [Start] - [Transforming]
#  2016-10-02 23:17:17 - [clean_data] - INFO - [End  ] - [Transforming] - [Done in 1 sec. (0 min.)]
# 2016-10-02 23:17:17 - [clean_data] - INFO - [End  ] - [Cleaning] - [Done in 1.5 sec. (0 min.)]

delta1epsilon/logtime documentation built on May 15, 2019, 3:22 a.m.