Skip to content

Commit 6b1d471

Browse files
committed
Fix #6 add api_logger
1 parent c74adbe commit 6b1d471

File tree

5 files changed

+141
-2
lines changed

5 files changed

+141
-2
lines changed

NAMESPACE

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export(api_get)
6565
export(api_get_header)
6666
export(api_head)
6767
export(api_head_header)
68+
export(api_logger)
6869
export(api_message)
6970
export(api_off)
7071
export(api_on)
@@ -83,6 +84,8 @@ export(api_statics)
8384
export(api_stop)
8485
export(api_trace)
8586
export(api_trace_header)
87+
export(combined_log_format)
88+
export(common_log_format)
8689
export(create_server_yml)
8790
export(device_formatter)
8891
export(format_bmp)
@@ -106,6 +109,11 @@ export(format_yaml)
106109
export(get_parsers)
107110
export(get_serializers)
108111
export(is_plumber_api)
112+
export(logger_console)
113+
export(logger_file)
114+
export(logger_logger)
115+
export(logger_null)
116+
export(logger_switch)
109117
export(parse_csv)
110118
export(parse_feather)
111119
export(parse_geojson)
@@ -126,6 +134,13 @@ import(rlang)
126134
importFrom(R6,R6Class)
127135
importFrom(base64enc,base64decode)
128136
importFrom(fiery,Fire)
137+
importFrom(fiery,combined_log_format)
138+
importFrom(fiery,common_log_format)
139+
importFrom(fiery,logger_console)
140+
importFrom(fiery,logger_file)
141+
importFrom(fiery,logger_logger)
142+
importFrom(fiery,logger_null)
143+
importFrom(fiery,logger_switch)
129144
importFrom(jsonlite,write_json)
130145
importFrom(ragg,agg_jpeg)
131146
importFrom(ragg,agg_png)

R/Plumber.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ Plumber <- R6Class(
111111

112112
check_environment(env)
113113
private$PARENT_ENV <- env
114-
115-
#self$set_logger(fiery::logger_console())
116114
},
117115
#' @description Human readable description of the api object
118116
#' @param ... ignored

R/api_logger.R

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#' Set logging function and access log format for the API
2+
#'
3+
#' Plumber has a build-in logging facility that takes care of logging any
4+
#' conditions that are caught, as well as access logs. Further it is possible to
5+
#' log custom messages using the `log()` method on the api object. However, the
6+
#' actual logging is handled by a customizable function that can be set. You can
7+
#' read more about the logging infrastructure in the
8+
#' [fiery documentation][fiery::loggers]. plumber2 reexports the loggers
9+
#' provided by fiery so they are immediately available to the user.
10+
#'
11+
#' @param api A plumber2 api object to set the logger on
12+
#' @param logger A logger function. If `NULL` then the current logger is kept
13+
#' @param access_log_format A glue string giving the format for the access logs.
14+
#' plumber2 (through fiery) provides the predefined `common_log_format` and
15+
#' `combined_log_format`, but you can easily create your own. See
16+
#' [fiery::loggers] for which variables the glue string has access to.
17+
#' @inheritParams fiery::loggers
18+
#'
19+
#' @export
20+
#'
21+
api_logger <- function(api, logger = NULL, access_log_format = NULL) {
22+
if (!is.null(logger)) {
23+
check_function(logger)
24+
api$set_logger(logger)
25+
}
26+
if (!is.null(access_log_format)) {
27+
check_string(access_log_format)
28+
api$access_log_format <- access_log_format
29+
}
30+
api
31+
}
32+
33+
#' @rdname api_logger
34+
#' @export
35+
#' @importFrom fiery logger_null
36+
fiery::logger_null
37+
#' @rdname api_logger
38+
#' @export
39+
#' @importFrom fiery logger_console
40+
fiery::logger_console
41+
#' @rdname api_logger
42+
#' @export
43+
#' @importFrom fiery logger_file
44+
fiery::logger_file
45+
#' @rdname api_logger
46+
#' @export
47+
#' @importFrom fiery logger_logger
48+
fiery::logger_logger
49+
#' @rdname api_logger
50+
#' @export
51+
#' @importFrom fiery logger_switch
52+
fiery::logger_switch
53+
#' @rdname api_logger
54+
#' @export
55+
#' @importFrom fiery common_log_format
56+
#' @format NULL
57+
fiery::common_log_format
58+
#' @rdname api_logger
59+
#' @export
60+
#' @importFrom fiery combined_log_format
61+
#' @format NULL
62+
fiery::combined_log_format

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ reference:
5555
- title: "Advanced"
5656
contents:
5757
- Plumber
58+
- api_logger
5859
- api_doc_add
5960
- api_on
6061
- api_session_cookie

man/api_logger.Rd

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)