From f56881efdae67229d3903e59d6446e23052fbe33 Mon Sep 17 00:00:00 2001
From: Stijn de Witt
Date: Thu, 8 Apr 2021 19:11:31 +0200
Subject: [PATCH] WIP Structured logging via a format Consider
anylogger-structured?
---
README.md | 36 ++++-----
core/grab.js | 2 +-
core/index.js | 39 ++++-----
core/merge.js | 2 +-
core/parse.js | 118 ----------------------------
mods/align/index.js | 4 -
mods/channels/index.js | 9 +--
mods/colors/index.js | 5 +-
mods/config/index.js | 6 +-
mods/config/read.js | 3 +-
mods/config/watched.js | 3 +-
mods/config/watches.js | 4 +-
mods/formats/apply-formatting.js | 2 +-
mods/formats/cr.js | 5 ++
mods/formats/default.js | 2 +-
mods/formats/index.js | 35 ++++++---
mods/formats/json.js | 5 ++
mods/formats/{message.js => msg.js} | 4 +-
mods/formats/structured.js | 18 +++++
mods/outputs/index.js | 6 +-
mods/props/index.js | 8 +-
mods/props/test.js | 1 +
mods/settings/index.js | 6 +-
style.css | 35 ++++++++-
tutorial/index.html | 21 +++--
tutorial/noscript.html | 10 +++
webpack.config.js | 2 +-
27 files changed, 171 insertions(+), 220 deletions(-)
delete mode 100644 core/parse.js
create mode 100644 mods/formats/cr.js
create mode 100644 mods/formats/json.js
rename mods/formats/{message.js => msg.js} (58%)
create mode 100644 mods/formats/structured.js
create mode 100644 tutorial/noscript.html
diff --git a/README.md b/README.md
index 08e597a..032ad1a 100644
--- a/README.md
+++ b/README.md
@@ -146,12 +146,12 @@ log('Logging is easy!')
If you want the file for the browser to include in your project yourself, you
can download it from here.
-* [ulog.min.js](https://unpkg.com/ulog@2.0.0-beta.18/ulog.min.js) (~2.7kB minified and gzipped)
-* [ulog.lazy.min.js](https://unpkg.com/ulog@2.0.0-beta.18/ulog.lazy.min.js) (~4.3kB minified and gzipped)
+* [ulog.min.js](https://unpkg.com/ulog@2.0.0-beta.18/ulog.min.js) (~8.0kB minified and gzipped)
+* [ulog.lazy.min.js](https://unpkg.com/ulog@2.0.0-beta.18/ulog.lazy.min.js) (~9.1kB minified and gzipped)
> `ulog.min.js` lazy loads `ulog.lazy.min.js` on demand, so make sure to include both files in your download
-* [full.min.js](https://unpkg.com/ulog@2.0.0-beta.18/full.min.js) (~5.7kB minified and gzipped)
+* [full.min.js](https://unpkg.com/ulog@2.0.0-beta.18/full.min.js) (~14.2kB minified and gzipped)
> Full bundle, no lazy loading
@@ -398,8 +398,8 @@ ulog.use({
outputs: {
custom: function(ctx){
return function(rec) {
- rec.message.shift('Custom!!')
- console[rec.level].apply(console, rec.message)
+ rec.msg.shift('Custom!!')
+ console[rec.level].apply(console, rec.msg)
}
}}
}
@@ -427,11 +427,11 @@ configuration mechanism.
The default format string on Node is:
```sh
-lvl name:20 message perf
+lvl name:20 msg perf
```
-This sacrifices the callstack for a colored and formatted `message` and having
-the `perf` measurements after the message i.s.o before it. The Node JS doesn't
+This sacrifices the callstack for a colored and formatted `msg` and having
+the `perf` measurements after the message i.s.o before it. Node JS doesn't
output any file name / line number information anyway.
On browsers, we want to spare the call stack, so there the default is:
@@ -440,7 +440,7 @@ On browsers, we want to spare the call stack, so there the default is:
lvl name perf
```
-We don't include the `message`, but it will be appended as the last argument
+We don't include the `msg`, but it will be appended as the last argument
automatically. The result is nicely formatted messages with the file name /
line number entries in the browser debug console intact.
@@ -452,7 +452,7 @@ Formats available out of the box include:
* [Format `cr`](#format-cr)
* [Format `date`](#format-date)
* [Format `lvl`](#format-lvl)
-* [Format `message`](#format-message)
+* [Format `msg`](#format-msg)
* [Format `name`](#format-name)
* [Format `perf`](#format-perf)
* [Format `time`](#format-time)
@@ -500,7 +500,7 @@ Prints the level of the message as a single character:
* `'>'` for debug messages
* `'}'` for trace messages
-#### Format `message`
+#### Format `msg`
Prints the message, formatted and colored.
Using this format breaks the callstack as it is dynamic.
@@ -538,7 +538,7 @@ import formats from 'ulog/mods/formats'
ulog.use({
use: [ formats ],
formats: {
- custom: (ctx) => (rec) => (['custom'].concat(rec.message)),
+ custom: (ctx) => (rec) => (['custom'].concat(rec.msg)),
random: (ctx, rec) => () => Math.random()
}
})
@@ -562,7 +562,7 @@ ulog.use({
dynamicFormat: function(ctx) {
// one-time init here
return function(rec) {
- // rec.message contains full message
+ // rec.msg contains full message
return /* ... */
}
}
@@ -582,8 +582,8 @@ ulog.use({
staticFormat: function(ctx, rec) {
// one-time init here
return function(){
- // rec.message is undefined
- // rec.name, rec.level etc is populated
+ // rec.msg is undefined
+ // rec.name, rec.level etc are populated
return /* ... */
}
}
@@ -781,12 +781,12 @@ log_drain=my:*,-my:lib=noop
### Config option `log_format`
-Specify the format to use. Defaults to `lvl name:22 message perf` on Node JS and `lvl name perf` on browsers.
+Specify the format to use. Defaults to `lvl name:22 msg perf` on Node JS and `lvl name perf` on browsers.
```sh
-log_format=lvl name perf message;my:*=lvl name perf
+log_format=lvl name perf msg;my:*=lvl name perf
```
-This sets `lvl name perf message` as the default format, while assigning a different format string to all loggers starting with `my:`.
+This sets `lvl name perf msg` as the default format, while assigning a different format string to all loggers starting with `my:`.
For more details, refer to the [section on formatting](#formatting)
diff --git a/core/grab.js b/core/grab.js
index d82b231..368e11a 100644
--- a/core/grab.js
+++ b/core/grab.js
@@ -1,4 +1,4 @@
-var merge = require('./merge')
+var merge = require('./merge')
module.exports = function(ulog, name, result) {
ulog.mods.reduce(function(r,mod){
diff --git a/core/index.js b/core/index.js
index f23e54b..4b01289 100644
--- a/core/index.js
+++ b/core/index.js
@@ -1,5 +1,5 @@
-var ulog = require('anylogger')
-var grab = require('./grab')
+var ulog = require('anylogger')
+var merge = require('./merge')
var ext = ulog.ext
@@ -18,10 +18,10 @@ var ext = ulog.ext
ulog.ext = function(logger) {
if (logger) {
ext(logger)
- grab(ulog, 'ext', []).map(function(ext){
+ ulog.grab('ext', []).map(function(ext){
ext.call(ulog, logger)
})
- grab(ulog, 'after', []).map(function(ext){
+ ulog.grab('after', []).map(function(ext){
ext.call(ulog, logger)
})
return logger
@@ -34,6 +34,18 @@ ulog.ext = function(logger) {
ulog.mods = []
+ulog.grab = function(name, result) {
+ this.mods.reduce(function(r,mod){
+ if (Array.isArray(r) && (name in mod)) {
+ r.push(mod[name])
+ } else {
+ merge(r, mod[name])
+ }
+ return r
+ }, result)
+ return result
+}
+
/**
* ### `ulog.use(mod: Object|Array
Because msg is a dynamic tag, ulog was
unable to preserve the callstack and the result is that now, the
filename and line numbers all point to ulog.lazy.min.js,
which is a lot less useful. But the message is now formatted and
diff --git a/tutorial/noscript.html b/tutorial/noscript.html
new file mode 100644
index 0000000..d01815e
--- /dev/null
+++ b/tutorial/noscript.html
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/webpack.config.js b/webpack.config.js
index a2b3d93..0be6c1d 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -19,7 +19,7 @@ module.exports = {
},
devtool: false,
optimization: {
- minimize: true,
+ minimize: false,
minimizer: [
new TerserPlugin({
extractComments: false,