-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
203 additions
and
178 deletions.
There are no files selected for viewing
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
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
This file was deleted.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,70 @@ | ||
import pino from 'pino' | ||
import pinoHttp from 'pino-http' | ||
import { subsystemLogger } from '@atproto/common' | ||
import * as pino from 'pino' | ||
import * as pinoHttp from 'pino-http' | ||
import * as jwt from 'jsonwebtoken' | ||
import { parseBasicAuth } from './auth' | ||
import * as xrpc from '@atproto/xrpc-server' | ||
|
||
export const dbLogger = subsystemLogger('pds:db') | ||
export const readStickyLogger = subsystemLogger('pds:read-sticky') | ||
export const redisLogger = subsystemLogger('pds:redis') | ||
export const seqLogger = subsystemLogger('pds:sequencer') | ||
export const mailerLogger = subsystemLogger('pds:mailer') | ||
export const labelerLogger = subsystemLogger('pds:labler') | ||
export const crawlerLogger = subsystemLogger('pds:crawler') | ||
export const httpLogger = subsystemLogger('pds') | ||
export type Logger = xrpc.Logger & { | ||
http: pino.Logger | ||
db: pino.Logger | ||
seq: pino.Logger | ||
redis: pino.Logger | ||
mailer: pino.Logger | ||
labeler: pino.Logger | ||
crawler: pino.Logger | ||
munge: pino.Logger | ||
} | ||
|
||
export const loggerMiddleware = pinoHttp({ | ||
logger: httpLogger, | ||
serializers: { | ||
req: (req) => { | ||
const serialized = pino.stdSerializers.req(req) | ||
const authHeader = serialized.headers.authorization || '' | ||
let auth: string | undefined = undefined | ||
if (authHeader.startsWith('Bearer ')) { | ||
const token = authHeader.slice('Bearer '.length) | ||
const sub = jwt.decode(token)?.sub | ||
if (sub) { | ||
auth = 'Bearer ' + sub | ||
} else { | ||
auth = 'Bearer Invalid' | ||
export const createLogger = ( | ||
opts: Partial<xrpc.LoggerOpts> = {}, | ||
): { logger: Logger; logMiddleware: pinoHttp.HttpLogger } => { | ||
const logger = xrpc.createLogger(opts) | ||
const http = logger.child({ name: 'pds' }) | ||
logger['http'] = http | ||
logger['db'] = logger.child({ name: 'pds:db' }) | ||
logger['seq'] = logger.child({ name: 'pds:sequencer' }) | ||
logger['redis'] = logger.child({ name: 'pds:redis' }) | ||
logger['mailer'] = logger.child({ name: 'pds:mailer' }) | ||
logger['labeler'] = logger.child({ name: 'pds:labeler' }) | ||
logger['crawler'] = logger.child({ name: 'pds:crawler' }) | ||
logger['munge'] = logger.child({ name: 'pds:munge' }) | ||
const logMiddleware = createLoggerMiddleware(http) | ||
return { logger: logger as Logger, logMiddleware } | ||
} | ||
|
||
export const createLoggerMiddleware = (logger: pino.Logger) => { | ||
return pinoHttp.default({ | ||
logger, | ||
serializers: { | ||
req: (req) => { | ||
const serialized = pino.stdSerializers.req(req) | ||
const authHeader = serialized.headers.authorization || '' | ||
let auth: string | undefined = undefined | ||
if (authHeader.startsWith('Bearer ')) { | ||
const token = authHeader.slice('Bearer '.length) | ||
const sub = jwt.decode(token)?.sub | ||
if (sub) { | ||
auth = 'Bearer ' + sub | ||
} else { | ||
auth = 'Bearer Invalid' | ||
} | ||
} | ||
if (authHeader.startsWith('Basic ')) { | ||
const parsed = parseBasicAuth(authHeader) | ||
if (!parsed) { | ||
auth = 'Basic Invalid' | ||
} else { | ||
auth = 'Basic ' + parsed.username | ||
} | ||
} | ||
} | ||
if (authHeader.startsWith('Basic ')) { | ||
const parsed = parseBasicAuth(authHeader) | ||
if (!parsed) { | ||
auth = 'Basic Invalid' | ||
} else { | ||
auth = 'Basic ' + parsed.username | ||
return { | ||
...serialized, | ||
headers: { | ||
...serialized.headers, | ||
authorization: auth, | ||
}, | ||
} | ||
} | ||
return { | ||
...serialized, | ||
headers: { | ||
...serialized.headers, | ||
authorization: auth, | ||
}, | ||
} | ||
}, | ||
}, | ||
}, | ||
}) | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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
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
Oops, something went wrong.