@timberio/core
Advanced tools
Comparing version 0.12.0 to 0.13.0
{ | ||
"name": "@timberio/core", | ||
"version": "0.12.0", | ||
"version": "0.13.0", | ||
"description": "Timber.io - logging core", | ||
@@ -40,5 +40,5 @@ "keywords": [ | ||
"dependencies": { | ||
"@timberio/types": "^0.12.0" | ||
"@timberio/types": "^0.13.0" | ||
}, | ||
"gitHead": "511550cfec37c90b6c3c8c6935a83faa4910d0a1" | ||
"gitHead": "9643811061b3c12daf7b094931bee1994c850b0b" | ||
} |
@@ -5,2 +5,74 @@ # 🌲 Timber - logging core | ||
> Docs TBA | ||
**New to Timber?** [Here's a low-down on logging in Javascript.](https://github.com/timberio/timber-js) | ||
## `@timberio/core` | ||
This is an NPM package that provides core logging functionality. | ||
It's used by the [Node](https://github.com/timberio/timber-js/tree/master/packages/node) and [browser](https://github.com/timberio/timber-js/tree/master/packages/browser) logging packages. | ||
You typically wouldn't require this package directly, unless you're building a custom logger. | ||
## The `Base` class | ||
The [Base](src/base.ts) class provides core features that is extended by loggers. | ||
For example - you could create a custom logger that implements its own sync method, for getting data over to [Timber.io](https://timber.io) | ||
```typescript | ||
import { Base } from "@timberio/core"; | ||
class CustomLogger extends Base { | ||
// Constructor must take a Timber.io API key | ||
public constructor(apiKey: string) { | ||
// Make sure you pass the API key to the parent constructor! | ||
super(apiKey); | ||
// Create a custom sync method | ||
this.setSync(async (log: ITimberLog) => { | ||
// Sync the `log` somehow ... `this._apiKey` contains your Timber API key | ||
// .... | ||
// Finally, return the log... which will resolve our initial `.log()` call | ||
return log; | ||
}); | ||
} | ||
} | ||
``` | ||
## The logging pipeline | ||
Logging to Timber is simple - just call the `.log()` function. | ||
```typescript | ||
timber.log({ message: "Hello Timber!", date: new Date() }); | ||
``` | ||
The `.log()` method returns a Promise, which resolves when the log has been synced with Timber.io | ||
You can add your own 'pipeline' middlware functions, which act as transforms on the passed `log: ITimberLog`. This is useful for adding your own logging middleware, or augmenting the log prior to syncing with Timber. | ||
For example, there's an implicit `preProcess` pipeline that adds an explicit date timestamp to a log that lacks one: | ||
```typescript | ||
async function preProcess(log: ITimberLog): Promise<ITimberLog> { | ||
return { | ||
date: new Date(), | ||
...log | ||
}; | ||
} | ||
``` | ||
You can add any number of pipeline functions to your logger instance (which will run in order): | ||
```typescript | ||
// Add a custom pipeline function - aka middleware | ||
timber.use(preProcess); | ||
``` | ||
Pipelines run _before_ the final sync to Timber.io. Pipeline functions should return a `Promise<ITimberLog>`, making it possible to augment logs, send to another destination, throw errors, etc. | ||
**Note: If an exception is thrown anywhere in the pipeline chain, the log _won't_ be synced. Wrap an async `try/catch` block around your call to `.log()` or tack on a `.catch()` to ensure your errors are handled.** | ||
> More docs TBA |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43506
78
+ Added@timberio/types@0.13.0(transitive)
- Removed@timberio/types@0.12.0(transitive)
Updated@timberio/types@^0.13.0