humid 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +23 -0
- data/lib/humid/version.rb +1 -1
- data/lib/humid.rb +6 -4
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 73079cacc72320a7c7d439d5cc443a42221a9e177dda5f25712c9898212f533a
|
|
4
|
+
data.tar.gz: f06abbe52e2e566a415d7982cac6c6313d158e1f47f2ec142179226f55d7cd63
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ce807ac8725f2054eaa15f00ecd2ec7532b5d720145f9078d211966fd3b5c81f06e282a9511778fa1ecfdcc4af028dd109ae950c14d6a9c4cb1f2c78d64bba14
|
|
7
|
+
data.tar.gz: 903b52f1aebf08c3f90d606f5e1eca7656aa04bc988d1342f863cf9a915acdc52b94d440d2f6f50c68f3d8e34f58d400c669dd7afc6f75688aad9104203e592c
|
data/README.md
CHANGED
|
@@ -123,6 +123,29 @@ The following functions are **not** available in the mini_racer environment
|
|
|
123
123
|
`console.log` and friends (`info`, `error`, `warn`) are delegated to the
|
|
124
124
|
respective methods on the configured logger.
|
|
125
125
|
|
|
126
|
+
All arguments are passed through — MiniRacer converts JS objects to Ruby
|
|
127
|
+
hashes and arrays automatically. A `log_formatter` proc controls how these
|
|
128
|
+
arguments are formatted into a single string for the logger:
|
|
129
|
+
|
|
130
|
+
```ruby
|
|
131
|
+
Humid.configure do |config|
|
|
132
|
+
config.logger = Rails.logger
|
|
133
|
+
|
|
134
|
+
config.log_formatter = proc { |level, message, *rest|
|
|
135
|
+
parts = [message]
|
|
136
|
+
parts += rest.map { |a| a.is_a?(String) ? a : JSON.pretty_generate(a) }
|
|
137
|
+
parts.join("\n")
|
|
138
|
+
}
|
|
139
|
+
end
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
The formatter receives `(level, message, *rest)` where:
|
|
143
|
+
- `level` — the log level as a symbol (`:debug`, `:info`, `:warn`, `:error`)
|
|
144
|
+
- `message` — the first argument passed to `console.log/info/warn/error`
|
|
145
|
+
- `rest` — any additional arguments (objects come through as Ruby hashes/arrays)
|
|
146
|
+
|
|
147
|
+
The default formatter simply returns `message` unchanged.
|
|
148
|
+
|
|
126
149
|
## Usage
|
|
127
150
|
|
|
128
151
|
In your entry file, e.g, `server_rendering.js`, pass your HTML render function
|
data/lib/humid/version.rb
CHANGED
data/lib/humid.rb
CHANGED
|
@@ -20,6 +20,7 @@ class Humid
|
|
|
20
20
|
self.config = ActiveSupport::OrderedOptions.new.merge({
|
|
21
21
|
raise_render_errors: true,
|
|
22
22
|
context_options: {},
|
|
23
|
+
log_formatter: proc { |_level, message, *_rest| message },
|
|
23
24
|
})
|
|
24
25
|
|
|
25
26
|
class << self
|
|
@@ -66,10 +67,11 @@ class Humid
|
|
|
66
67
|
ctx = MiniRacer::Context.new(**config.context_options)
|
|
67
68
|
|
|
68
69
|
if logger
|
|
69
|
-
|
|
70
|
-
ctx.attach("console.
|
|
71
|
-
ctx.attach("console.
|
|
72
|
-
ctx.attach("console.
|
|
70
|
+
fmt = config.log_formatter || proc { |_level, message, *_rest| message }
|
|
71
|
+
ctx.attach("console.log", proc { |*args| logger.debug(fmt.call(:debug, *args)) })
|
|
72
|
+
ctx.attach("console.info", proc { |*args| logger.info(fmt.call(:info, *args)) })
|
|
73
|
+
ctx.attach("console.error", proc { |*args| logger.error(fmt.call(:error, *args)) })
|
|
74
|
+
ctx.attach("console.warn", proc { |*args| logger.warn(fmt.call(:warn, *args)) })
|
|
73
75
|
end
|
|
74
76
|
|
|
75
77
|
js = ""
|