This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const controller = (req, res, next) => { | |
loggerCls.info('Long live rocknroll!') | |
// Logs something like | |
// {"level":30,"time":1551385666046,"msg":"Long live rocknroll!","pid":25,"hostname":"eb6a6c70f5c4","traceID":"9ba393f0-ec8c-4396-8092-b7e4b6f375b5","v":1} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cls = require('cls-hooked') | |
const uuidv4 = require('uuid/v4') | |
const clsMiddleware = (req, res, next) => { | |
// req and res are event emitters. We want to access CLS context inside of their event callbacks | |
clsNamespace.bind(req) | |
clsNamespace.bind(res) | |
const traceID = uuidv4() | |
const loggerWithTraceId = logger.child({ traceID }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pino = require('pino') | |
const logger = pino() | |
const loggerCls = new Proxy(logger, { | |
get(target, property, receiver) { | |
// Fallback to our original logger if there is no child logger in CLS | |
target = clsNamespace.get('loggerCls') || target | |
return Reflect.get(target, property, receiver) | |
}, | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { createLogger, format, transports } = require('winston') | |
const addTraceId = printf((info) => { | |
let message = info.message | |
const traceID = clsNamespace.get('taceID') | |
if (traceID) { | |
message = `[TraceID: ${traceID}]: ${message}` | |
} | |
return message | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const controller = (req, res, next) => { | |
const traceID = clsNamespace.get('traceID') | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cls = require('cls-hooked') | |
const uuidv4 = require('uuid/v4') | |
const clsNamespace = cls.createNamespace('app') | |
const clsMiddleware = (req, res, next) => { | |
// req and res are event emitters. We want to access CLS context inside of their event callbacks | |
clsNamespace.bind(req) | |
clsNamespace.bind(res) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We move that generic code to a dedicated module | |
import { Action, createClassReducer } from 'utils/reducer-class' | |
const actionTypeJediCreateInit = 'jedi-app/jedi-create-init' | |
const actionTypeJediCreateSuccess = 'jedi-app/jedi-create-success' | |
const actionTypeJediCreateError = 'jedi-app/jedi-create-error' | |
class ReducerJedi { | |
// Take a look at "Class field delcaratrions" proposal, which is now at Stage 3. | |
// https://github.com/tc39/proposal-class-fields |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reducerJedi = createClassReducer(ReducerJedi) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createReducer } from 'redux-create-reducer' | |
const createClassReducer = (ReducerClass) => { | |
const reducerClass = new ReducerClass() | |
const methodsWithActionTypes = getReducerClassMethodsWthActionTypes( | |
reducerClass, | |
) | |
const reducerMap = getReducerMap(methodsWithActionTypes) | |
const initialState = reducerClass.initialState | |
const reducer = createReducer(initialState, reducerMap) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getReducerMap = (methodsWithActionTypes) => | |
methodsWithActionTypes.reduce((reducerMap, { method, actionType }) => { | |
reducerMap[actionType] = method | |
return reducerMap | |
}, {}) |
NewerOlder