From 35f0d008544a88df88ed1e3a3f39cf7a16cccca7 Mon Sep 17 00:00:00 2001 From: Stijn de Witt Date: Fri, 23 Feb 2024 18:09:11 +0100 Subject: [PATCH 1/5] 1.1.0-beta.1: enhance adapter semantics --- LICENSE | 2 +- README.md | 314 ++++++++++++++++++++++++++------------------- anylogger.ts | 316 ++++++++++++++++++++++++++++------------------ build.js | 17 ++- package-lock.json | 33 ++++- package.json | 16 +-- 6 files changed, 435 insertions(+), 263 deletions(-) diff --git a/LICENSE b/LICENSE index 6a6567d..d1b128e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2020 Stijn de Witt +Copyright (c) 2024 Stijn de Witt Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index d049f90..3516a85 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# anylogger 1.0.11 +# anylogger 1.1.0-beta.1 ### Get a logger. Any logger. [![npm](https://img.shields.io/npm/v/anylogger.svg)](https://npmjs.com/package/anylogger) @@ -30,20 +30,23 @@ just that.
Install
-
npm i -P anylogger
+
npm i -D anylogger
Use
import anylogger from 'anylogger'
 const log = anylogger('my-library')
 log('Anylogger is easy!')
-

Install your preferred logger - and it's adapter as dev dependencies.

+

Add to peerDependencies and install + an adapter as dev dependencies.

Install
npm i -P anylogger
  debug anylogger-debug
Add to entry point
index.js -
import "anylogger-debug"
+
import adapter from "anylogger-debug"
+import anylogger from 'anylogger'
+adapter(anylogger)
Use
import anylogger from 'anylogger'
 const log = anylogger('my-app')
@@ -54,7 +57,9 @@ log('Anylogger is easy!')
loglevel anylogger-loglevel
Add to entry point
index.js -
import "anylogger-loglevel"
+
import adapter from "anylogger-loglevel"
+import anylogger from 'anylogger'
+adapter(anylogger)
Use
import anylogger from 'anylogger'
 const log = anylogger('my-app')
@@ -65,7 +70,9 @@ log('Anylogger is easy!')
ulog
Add to entry point
index.js -
import "ulog"
+
import adapter from "ulog"
+import anylogger from 'anylogger'
+adapter(anylogger)
Use
import anylogger from 'anylogger'
 const log = anylogger('my-app')
@@ -76,7 +83,9 @@ log('Anylogger is easy!')
log4js anylogger-log4js
Add to entry point
index.js -
import "anylogger-log4js"
+
import adapter from "anylogger-log4js"
+import anylogger from 'anylogger'
+adapter(anylogger)
Use
import anylogger from 'anylogger'
 const log = anylogger('my-app')
@@ -107,37 +116,37 @@ for popular loggers.
 
 ### Introducing `anylogger`
 
-A tiny ~[264](#gzip-size) bytes logging facade that you can include in your
+A tiny ~[259](#gzip-size) bytes logging facade that you can include in your
 library to support logging, while at the same time allowing application
 developers to plug in any logging framework they choose.
 
 Instead of building in your own library specific configuration mechanism,
 or forcing the choice for a certain logging framework on your users,
 or just abandoning logging altogether, choose `anylogger` and for just
-~[264](#gzip-size) bytes shared between all libraries doing this, we can
+~[259](#gzip-size) bytes shared between all libraries doing this, we can
 plug in any framework of our choice and all libraries will automatically
 start to use that framework. Wouldn't it be much better and easier?
 
 
 ## Download
 
-* [anylogger.ts](https://unpkg.com/anylogger@1.1.0-beta.0/anylogger.ts)
-  (fully commented source ~6kB)
-* [anylogger.d.ts](https://unpkg.com/anylogger@1.1.0-beta.0/anylogger.d.ts)
-  (typescript type definitions ~5kB)
-* [anylogger.js](https://unpkg.com/anylogger@1.1.0-beta.0/anylogger.js)
-  (transpiled es6 ecmascript module ~1kB)
-* [anylogger.cjs](https://unpkg.com/anylogger@1.1.0-beta.0/anylogger.cjs)
-  (transpiled es6 commonjs module ~1kB)
-* [anylogger.min.js](https://unpkg.com/anylogger@1.1.0-beta.0/anylogger.min.js)
-  (minified 361 bytes, gzipped ~[264](#gzip-size) bytes)
+* [anylogger.ts](https://unpkg.com/anylogger@1.1.0-beta.1/anylogger.ts)
+  (fully commented source ~7kB)
+* [anylogger.d.ts](https://unpkg.com/anylogger@1.1.0-beta.1/anylogger.d.ts)
+  (typescript type definitions ~6kB)
+* [anylogger.js](https://unpkg.com/anylogger@1.1.0-beta.1/anylogger.js)
+  (javascript esm module ~2kB)
+* [anylogger.cjs](https://unpkg.com/anylogger@1.1.0-beta.1/anylogger.cjs)
+  (javascript commonjs module ~2kB)
+* [anylogger.min.js](https://unpkg.com/anylogger@1.1.0-beta.1/anylogger.min.js)
+  (minified 355 bytes, gzipped ~[259](#gzip-size) bytes)
 
 
 ## CDN
 
 *index.html*
 ```html
-
+
 
+
 
+
 
+
 
+
 (function(){ // IIFE
   var log = anylogger('index.html')
   log.info('Logging is simple!')
@@ -666,7 +666,7 @@ The log function returned by anylogger calls `anylogger.log`, which determines
 the log level and invokes the appropriate log method.
 
 Please have a look at the
-[source](https://unpkg.com/anylogger@1.1.0-beta.4/anylogger.js)
+[source](https://unpkg.com/anylogger@1.1.0-beta.5/anylogger.js)
 it should make it more clear how to write an adapter. Also consider studying
 the [available adapters](https://www.npmjs.com/search?q=keywords:anylogger)
 and learn by example.
diff --git a/anylogger.ts b/anylogger.ts
index 4321068..b1e12e2 100644
--- a/anylogger.ts
+++ b/anylogger.ts
@@ -143,20 +143,17 @@ export type HasExtension = {
  */
 export type Extension = (logfn: LogFunction) => Logger
 
-// a default no-op extension
-const noop: Extension = (logfn: LogFunction): Logger => {
+// anylogger.ext extends the given `logger` function
+// the implementation here only adds no-ops
+// adapters should change this behavior
+anylogger.ext = (logfn) => {
   (logfn as Logger).enabledFor = ()=>{}
   for (const lvl in anylogger.levels) {
-    (logfn as Logger)[lvl as LogLevel] = ()=>{}
+    (logfn as any)[lvl as LogLevel] = ()=>{}
   }
   return logfn as Logger
 }
 
-// anylogger.ext extends the given `logger` function
-// the implementation here only adds no-ops
-// adapters should change this behavior
-anylogger.ext = noop
-
 /**
  * An Adapter accepts the `anyLogger` function and optionally
  * some logging framework and adapts anylogger to that logging
@@ -202,10 +199,9 @@ export type HasLevels = {
 export type LogLevel = string
 
 /**
- * A mapping of level name `string` to level `number` that consists
- * of at least the keys from `logLevels` plus arbitrary other keys
+ * A mapping of level name `string` to level `number`
  */
-export type LogLevels = typeof logLevels & { [level: string]: number }
+export type LogLevels = typeof logLevels
 
 /**
  * A default set of level name/value pairs that maps well to the console.
@@ -268,10 +264,10 @@ export type Delegate = (name: string, ...args: any[]) => void
 // logs with the logger with the given `name`
 anylogger.log = (name, ...args) => (
   // select the logger to use
-  anylogger(name)[
+  (anylogger(name) as any)[
     // select the level to use
-    // if multiple args and first matches a level name
-    (((args.length > 1) && anylogger.levels[args[0] as LogLevel])
+    // if multiple args and first arg matches a level name
+    (((args.length > 1) && (anylogger as any).levels[args[0] as LogLevel])
       ? args.shift() // use the level from the args
       : 'log'   // else use default level `'log'`
     ) as LogLevel
diff --git a/package.json b/package.json
index c262cd7..aed4dc2 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "anylogger",
-  "version": "1.1.0-beta.4",
+  "version": "1.1.0-beta.5",
   "description": "Get a logger. Any logger.",
   "type": "module",
   "exports": {