Skip to content

Commit

Permalink
Merge pull request #15 from danielolaviobr/match-types-with-pino
Browse files Browse the repository at this point in the history
Changing types names and adding new types to match pino
  • Loading branch information
chasers authored Mar 12, 2021
2 parents 4f99496 + a8b39dd commit 94e296a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
39 changes: 28 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import createHttpWriteStream from "./httpStream"
import createConsoleWriteStream from "./consoleStream"
import {pinoBrowserLogEventI, formatPinoBrowserLogEvent, addLogflareTransformDirectives} from "./utils"
import {LogflareHttpClient, LogflareUserOptionsI} from "logflare-transport-core"
import {
LogEvent,
formatPinoBrowserLogEvent,
addLogflareTransformDirectives,
} from "./utils"
import {
LogflareHttpClient,
LogflareUserOptionsI,
} from "logflare-transport-core"

const isBrowser = typeof window !== 'undefined'
&& typeof window.document !== 'undefined'
const isBrowser =
typeof window !== "undefined" && typeof window.document !== "undefined"

const isNode = typeof process !== 'undefined'
&& process.versions != null
&& process.versions.node != null
const isNode =
typeof process !== "undefined" &&
process.versions != null &&
process.versions.node != null

const createPinoBrowserSend = (options: LogflareUserOptionsI) => {
const client = new LogflareHttpClient({...options, fromBrowser: true})
const client = new LogflareHttpClient({ ...options, fromBrowser: true })

return (level: string, logEvent: pinoBrowserLogEventI) => {
return (level: string, logEvent: LogEvent) => {
const logflareLogEvent = formatPinoBrowserLogEvent(logEvent)
const maybeWithTransforms = addLogflareTransformDirectives(logflareLogEvent, options)
const maybeWithTransforms = addLogflareTransformDirectives(
logflareLogEvent,
options
)
client.postLogEvents([maybeWithTransforms])
}
}
Expand All @@ -29,4 +40,10 @@ const logflarePinoVercel = (options: LogflareUserOptionsI) => {

const createWriteStream = createHttpWriteStream

export {createWriteStream, logflarePinoVercel, createPinoBrowserSend, createConsoleWriteStream, createHttpWriteStream}
export {
createWriteStream,
logflarePinoVercel,
createPinoBrowserSend,
createConsoleWriteStream,
createHttpWriteStream,
}
24 changes: 18 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,26 @@ function levelToStatus(level: number) {
return "info"
}

interface pinoBrowserLogEventI {
type Level = "fatal" | "error" | "warn" | "info" | "debug" | "trace"
type SerializerFn = (value: any) => any

interface Bindings {
level?: Level | string
serializers?: { [key: string]: SerializerFn }
[key: string]: any
}

interface LogEvent {
ts: number
messages: string[]
bindings: object[]
level: { value: number; label: string }
messages: any[]
bindings: Bindings[]
level: {
label: string
value: number
}
}

const formatPinoBrowserLogEvent = (logEvent: pinoBrowserLogEventI) => {
const formatPinoBrowserLogEvent = (logEvent: LogEvent) => {
const {
ts,
messages,
Expand Down Expand Up @@ -107,6 +119,6 @@ function toLogEntry(item: Record<string, any>): Record<string, any> {
export {
toLogEntry,
formatPinoBrowserLogEvent,
pinoBrowserLogEventI,
LogEvent,
addLogflareTransformDirectives,
}

0 comments on commit 94e296a

Please sign in to comment.