Skip to content

Instantly share code, notes, and snippets.

View hbarcelos's full-sized avatar
🕶️
I know how to exit Vim :v

Henrique Barcelos hbarcelos

🕶️
I know how to exit Vim :v
View GitHub Profile
@hbarcelos
hbarcelos / async-data.js
Created September 24, 2016 18:39
Processing asynchronously pushed data
// Originally from: http://exploringjs.com/es6/ch_generators.html#_example-processing-asynchronously-pushed-data
'use strict'
const createReadStream = require('fs').createReadStream
function coroutine(generatorFunction) {
return function (...args) {
const generatorObject = generatorFunction(...args);
generatorObject.next()
return generatorObject
  • Node: v6.10.3
  • NPM: 3.10.10
npm ls --depth 1
proxy-geo-router@1.0.0 /home/henrique/development/revmobads/proxy-geo-router
├── @istanbuljs/nyc-config-babel@1.2.2
├─┬ ava@0.19.1
@hbarcelos
hbarcelos / remove-dbs.js
Created February 22, 2018 17:51
Remove all user-created DBs from mongo.
db.getMongo().getDBNames()
.filter(function(db) {
return ['admin', 'config', 'local'].indexOf(db) === -1
})
.forEach(function (dbName) {
db.getMongo().getDB(dbName).dropDatabase();
})
@hbarcelos
hbarcelos / log-cls-example.js
Last active August 8, 2018 20:22
Logging with CLS
const express = require('express');
const pino = require('pino');
const cuid = require('cuid');
const { createNamespace, getNamespace } = require('cls-hooked');
const logger = pino();
const loggerNamespace = createNamespace('logger');
const app = express();
@hbarcelos
hbarcelos / logger.js
Last active August 25, 2018 19:09
Contextual Logger - Initial Implementation
const pino = require('pino');
function createLogger(opts, destination) {
return pino(opts, destination);
}
module.exports = createLogger;
@hbarcelos
hbarcelos / logger.tests.js.diff
Last active August 25, 2018 19:31
Contextual Logger - Keep context from method call
diff --git a/logger.test.js b/logger.test.js
index 8f89251..a841156 100644
--- a/logger.test.js
+++ b/logger.test.js
@@ -13,3 +13,19 @@ loggerMethodUseCases.forEach(({ input, expected }) => {
expected
);
});
+
+test(`Properly logs message with context object`, async t => {
@hbarcelos
hbarcelos / logger.js.diff
Created August 25, 2018 19:44
Contextual Logger - Added CLS awareness
diff --git a/logger.js b/logger.js
index f78dff6..dbac73a 100644
--- a/logger.js
+++ b/logger.js
@@ -1,7 +1,11 @@
const pino = require('pino');
+const { createNamespace } = require('cls-hooked');
function createLogger(opts, destination) {
- return pino(opts, destination);
@hbarcelos
hbarcelos / logger.js.diff
Created August 25, 2018 20:02
Contextual Logger - CLS namespace differentiation
diff --git a/logger.js b/logger.js
index dbac73a..3aa3418 100644
--- a/logger.js
+++ b/logger.js
@@ -1,9 +1,13 @@
const pino = require('pino');
const { createNamespace } = require('cls-hooked');
+let counter = 0;
+
@hbarcelos
hbarcelos / logger.js.diff
Created August 25, 2018 20:53
Contextual Logger - Applying CLS context to logs
diff --git a/logger.js b/logger.js
index 3aa3418..d19e435 100644
--- a/logger.js
+++ b/logger.js
@@ -1,6 +1,41 @@
const pino = require('pino');
const { createNamespace } = require('cls-hooked');
+const logMethods = ['trace', 'debug', 'info', 'warn', 'error', 'fatal'];
+
@hbarcelos
hbarcelos / logger.js.diff
Created August 25, 2018 21:58
Contextual Logger - Propagating changes to children
diff --git a/logger.js b/logger.js
index d19e435..d2740cd 100644
--- a/logger.js
+++ b/logger.js
@@ -1,8 +1,6 @@
const pino = require('pino');
const { createNamespace } = require('cls-hooked');
-const logMethods = ['trace', 'debug', 'info', 'warn', 'error', 'fatal'];
-