Socket
Socket
Sign inDemoInstall

filter-log

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

filter-log - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

examples/gen-random-logs.js

2

filter-log-command.js

@@ -11,3 +11,3 @@ #!/usr/local/bin/node

if(argv.h || argv.help || process.stdin.isTTY) {
fs.createReadStream('./docs/command-line-help.txt').pipe(process.stdout)
fs.createReadStream(require.resolve('./docs/command-line-help.txt')).pipe(process.stdout)
return

@@ -14,0 +14,0 @@ }

@@ -137,3 +137,3 @@ var _ = require('underscore')

filterLog.createStdOutProcessor = function() {
filterLog.defineProcessor('std-out')
filterLog.defineProcessor('std-out', {}, process.stdout)
}

@@ -149,2 +149,6 @@

filterLog.removeProcessor = function(name) {
delete logsProc[name]
}
filterLog.baseInformationGenerator = function() {

@@ -151,0 +155,0 @@ return {

@@ -5,3 +5,3 @@ var levels = {

WARN: 30,
ERROR: 49,
ERROR: 40,
FATAL: 50,

@@ -8,0 +8,0 @@ OFF: 60

{
"name": "filter-log",
"version": "0.0.2",
"version": "0.0.3",
"description": "flexible, minimalistic logging",

@@ -8,3 +8,5 @@ "main": "filter-log.js",

"test": "node_modules/mocha/bin/mocha",
"testDebug": "node_modules/mocha/bin/mocha --inspect --debug-brk"
"testDebug": "node_modules/mocha/bin/mocha --inspect --debug-brk",
"gen-random-logs": "./examples/gen-random-logs.js > examples/gen-random-logs.json",
"process-1": "cat examples/gen-random-logs.json | ./examples/process-1.js > examples/process-1.txt"
},

@@ -31,3 +33,4 @@ "repository": {

"chai": "^4.0.2",
"mocha": "^3.4.2"
"mocha": "^3.4.2",
"random-strings": "0.0.1"
},

@@ -34,0 +37,0 @@ "dependencies": {

@@ -12,4 +12,10 @@ # filter-log

Filter log separates those concerns using the event bus pattern. Code that produces log entries get tools that make event log entries easy to create and informational. The application can set up how log events will be captured and saved. Log events are JS objects so we can save lots of information.
Filter Log separates those concerns using the event bus pattern. Code that produces log entries just needs tools that make event log entries information and easy to create. The application can set up how log entries will be captured and saved. Log entries are JS objects so we can save lots of information.
## Install
```
npm install filter-log
```
Also, if installing globally and you want the man documentation for the command line client, run `sudo ./scripts/install-man`.
## The Basic Boring Example

@@ -19,3 +25,3 @@

var filog = require('filter-log')
filog.defineProcessor(process.stdout)
filog.defineProcessor('standard', {}, process.stdout)
var log = filog()

@@ -25,3 +31,3 @@ log.info('hello, world!')

Here we've done the basics. Line 2 creates a processor which is listening for log entries. It has no filter and will log absolutely everything. Since no transformer was specfied, the log entries will be formatted as streamable JSON.
Here we've done the basics. Line 2 creates a processor which is listening for log entries. It has no filter and will log absolutely everything. It's formatting the entries as streamable JSON strings (the default).

@@ -40,7 +46,7 @@ Line 3 creates a new logger. It has the basic tools you'd expect for logging by level and string interpolation. Line 4 creates a new log entry, sets the level to "info" and publishes it.

The object above will be augmented with date information and (given the processor set up in the first example) logged to std out. Filter log loggers are streams. You can write and pipe data to them like normal streams.
The object above will be augmented with date information and (given the processor set up in the first example) logged to std out. Filter Log loggers are streams. You can write and pipe data to them like normal streams.
## Letting the Logger Help
Lots of times the logger will have contextual information you want to save with every log entry. This could be just the name of the component creating the log entry or could be selected configuration items that are determing behavior or environmental information that will help with debugging. Let's create a logger which with will help us keep our code DRY.
Lots of times the logger will have contextual information you want to save with every log entry. This could be just the name of the component creating the log entry or could be selected configuration items that are determining behavior (like platform) or environmental information that will help with debugging (request id). Let's create a logger which with will help us keep our code DRY.

@@ -53,9 +59,81 @@

A couple new things. When we created the logger on line 1, we gave it a name, `my-auth-component`. This will be used to identify all log entries created through this logger. We also set a single property that will be added to every log entry, the `hostName`. On line 2, we've used string interpolation to make it a little easier to create a message.
A couple new things in this example. When we created the logger on line 1, we gave it a name, `my-auth-component`. This will be used to identify all log entries created through this logger. We also set a single property that will be added to every log entry, the `hostName`. On line 2, we've used string interpolation to make it a little easier to create a message. If an entry is created with a string, it will still show up in the destination as a JSON object. The entry will have a member of `msg` which contains the string.
The last example was something the code creating the log entries might do. What if the application environment wanted to add extra info to the log entries? What if for debugging we needed some information that the code author didn't know would be needed. It would do it like this:
```
filog.defineLoggerBaseData('my-auth-component', { appVariable: varValue })
```
If a logger is every created with the name `my-auth-component`, its entries will contain the extra `appVariable` information.
## Doing Something with Log Entries
In our first example we created a processor which logs absolutely everything. That might be a shocking amount of trace data. Instead of that, what if we only wanted to log the error level information?
```
var filog = require('filter-log')
filog.defineProcessor(
'standard',
{},
process.stdout,
function(entry) {
return entry.level && entry.level >= filog.levels.ERROR
}
)
```
What if we wanted info level and above logs from the `http-requests` logger to go to a file?
```
var filog = require('filter-log')
var fs = require('fs')
filog.defineProcessor(
'requests-processor',
{},
fs.createWriteStream('./access-log.JSON'),
function(entry) {
return entry.level && entry.level >= filog.levels.INFO && entry.loggerName === 'http-requests'
}
)
```
Arguments are:
1. The name of the processor
2. Any additional data which should be included with the log entry
3. The destination where the log entries will go (a stream)
4. A function which returns true if the entry should be included (optional, may also be a transform stream). By default all entries are included.
5. A formatter (optional, a function or transform stream) which will take the JS object and transform it into a form the destination stream wants. By default, if the destination is a character stream, this is a JSON serialization of the JS object to a streamable JSON format. If the destination is an object mode stream, it doesn't transform the object at all.
Basically, the whole concept of filter-log is that creating, filtering, serializing, and writing info is dead simple in JavaScript. All that's really needed is a couple pieces of framework goo to tie them together and provide defaults.
## MISC Management
To get rid of a processor you can call `filog.removeProcessor(name)` to remove a specific processor or `filog.clearProcessors()` to remove them all.
`filog.baseInformationGenerator` is a function run to generate the dynamic information for the log entry, like the current time. It's safe to override but will get run for every log entry created, so don't put anything expensive in there. Just return any object.
If you want to enter an object to be logged without benefit of going through any of the loggers or having it timestamped, you can have it processed by calling `filog.writeToProcessors(entry)`.
As a shortcut to defining a "log everything" processor, you can call `filog.createStdOutProcessor()`.
## Log Post-processing
The pattern with JSON based logging is to save a gross amount of data and filter that down to what you want to see. Filter Log contains a command line utility `filter-log` which can do some basic filtering and formatting. You can check out its man page for exact capabilities. Basic usage is:
```
filter-log -h
```
or
```
cat my-log.json | filter-log > my-log.txt
```
Anything complicated though, and it may be much easier to write your own tiny program to process the entries. `exmaples/process-1.js` is a simple example. The source for `filter-log-command.js` may also serve as an example.
var through2 = require('through2')
var stream = require('stream');
// var stream = require('stream');
// var createStream = function() {
// return new stream.Transform({objectMode: true})
// }
//
// module.exports = createStream
var createStream = function() {
return new stream.Transform({objectMode: true})
var stream = through2({ objectMode: true }, function(chunk, enc, callback) {
this.push(chunk)
callback()
})
return stream
}
module.exports = createStream

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc