Skip to content

Commit

Permalink
feat: removed pino as a dependency (#58)
Browse files Browse the repository at this point in the history
* chore: removed pino as a dependency

* chore: move types/pino to devDependencies
  • Loading branch information
prafed authored Jun 10, 2022
1 parent 8914487 commit a6d43ad
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
4 changes: 1 addition & 3 deletions packages/sls-env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
"homepage": "https://github.com/cabiri-io/sls-pipe#readme",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"dependencies": {
"@types/pino": "^6.3.5"
},
"devDependencies": {
"@types/pino": "^6.3.5",
"pino": "^6.11.0"
},
"gitHead": "aa7cfbac2b84adad9b536f8efedfde9446389056"
Expand Down
4 changes: 2 additions & 2 deletions packages/sls-env/src/__tests__/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('serverless environment', () => {
return new TestLogger(context)
}

level = 'info'
level = 'info' as const
}

//@ts-expect-error
Expand All @@ -56,7 +56,6 @@ describe('serverless environment', () => {
it('creates a mutable logger', () => {
const mutableLogger = createMutableLogger(logger)

//@ts-expect-error
mutableLogger.child?.('test')
mutableLogger.debug('message')

Expand Down Expand Up @@ -176,6 +175,7 @@ describe('serverless environment', () => {
it('checks compatibility with pino logger', done => {
type Config = { message: string }
type Dependencies = { message: string }

environment<Handler<EmptyEvent, EmptyContext, void>, Config, Dependencies, string>({
logger: { mutable: true }
})
Expand Down
54 changes: 32 additions & 22 deletions packages/sls-env/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
import type { Logger as PinoLogger } from 'pino'
type LogLevel = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace'

type LogLevel = 'error' | 'warn' | 'info' | 'debug' | 'trace'
interface LogFn {
<T extends Record<string, unknown>>(obj: T, msg?: string, ...args: Array<any>): void
(msg: string, ...args: Array<any>): void
}

type Logger = {
error: PinoLogger['error']
warn: PinoLogger['warn']
info: PinoLogger['info']
debug: PinoLogger['debug']
trace: PinoLogger['trace']
child?: PinoLogger['child']
} & { [key in string]: any }
interface Logger {
/**
* Contains the desired logging level
*/
level?: string
fatal: LogFn
error: LogFn
warn: LogFn
info: LogFn
debug: LogFn
trace: LogFn
/**
* Support for certain loggers that create a child logger instance
*
* @returns a child logger instance.
*/
child?: (...args: Array<any>) => Logger
}

const objectToJson = (a: unknown) => (typeof a === 'object' ? JSON.stringify(a) : a)

type CreateLoggerConfig = { level?: string }

type CreateLogger = (config?: CreateLoggerConfig) => Logger
type CreateLogger = (config?: Record<string, unknown>) => Logger

const log = (level: LogLevel, logLevel: string, args: Array<unknown>): void => {
const log = (level: Exclude<LogLevel, 'fatal'>, logLevel: string, args: Array<unknown>): void => {
// eslint-disable-next-line no-console
if (level === logLevel) console[level](args.map(objectToJson).join(', '))
}

//@ts-expect-error
const defaultLoggerConstructor: CreateLogger = ({ level } = { level: 'info' }) => {
const logLevel = level ?? 'info'
const logLevel = (level ?? 'info') as LogLevel
const logger = {
fatal: (...args: Array<unknown>) => log('error', logLevel, args),
error: (...args: Array<unknown>) => log('error', logLevel, args),
warn: (...args: Array<unknown>) => log('warn', logLevel, args),
info: (...args: Array<unknown>) => log('info', logLevel, args),
Expand All @@ -39,9 +50,8 @@ const defaultLoggerConstructor: CreateLogger = ({ level } = { level: 'info' }) =

const defaultLogger = defaultLoggerConstructor()

const isFunction = (v: CreateLogger | Logger): v is CreateLogger => typeof v === 'function' && v instanceof Function

type LoggerConstructor = Logger | CreateLogger
const isFunction = (v: LoggerConstructor): v is CreateLogger => typeof v === 'function' && v instanceof Function

const createLogger = (level: string) => (loggerConstrutor: LoggerConstructor): Logger => {
if (isFunction(loggerConstrutor)) {
Expand All @@ -54,13 +64,13 @@ const createMutableLogger = (logger: Logger): Logger => {
let wrapperedLogger = logger

const handler = {
get(_: unknown, property: string) {
get(_: unknown, property: keyof Logger) {
if (typeof wrapperedLogger[property] === 'function') {
return (...args: Array<unknown>) => {
if (property === 'child' && wrapperedLogger?.[property]) {
//@ts-expect-error
if (property === 'child' && wrapperedLogger?.child) {
wrapperedLogger = wrapperedLogger?.[property]?.(...args) ?? wrapperedLogger
} else if (wrapperedLogger[property]) {
//@ts-expect-error
return wrapperedLogger[property](...args)
} else {
return
Expand All @@ -69,7 +79,7 @@ const createMutableLogger = (logger: Logger): Logger => {
}
return wrapperedLogger[property]
},
set(_: unknown, property: string, value: unknown) {
set(_: unknown, property: keyof Logger, value: any) {
wrapperedLogger[property] = value
return true
}
Expand Down

0 comments on commit a6d43ad

Please sign in to comment.