-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transports): add custom tranporter support for flexible logging
Introduces a new custom transporter feature to Logixlysia, allowing users to send logs to various destinations fix #51
- Loading branch information
Showing
8 changed files
with
168 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Elysia } from 'elysia' | ||
|
||
import logixlysia from '~/index' | ||
|
||
import MyCustomTransport from './myCustomTransport' | ||
|
||
const app = new Elysia() | ||
.use( | ||
logixlysia({ | ||
config: { | ||
transports: [new MyCustomTransport()] | ||
} | ||
}) | ||
) | ||
.get('/', () => { | ||
return { | ||
message: 'Basic Example' | ||
} | ||
}) | ||
|
||
app.listen(3000) |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { LogData, LogLevel, RequestInfo, StoreData, Transport } from '~/types' | ||
|
||
class MyCustomTransport implements Transport { | ||
async log( | ||
level: LogLevel, | ||
message: string, | ||
meta: { request: RequestInfo; data: LogData; store: StoreData } | ||
): Promise<void> { | ||
console.log(`Custom log: ${level} - ${message} - ${meta.request.method}`) | ||
} | ||
} | ||
|
||
export default MyCustomTransport |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { buildLogMessage } from '~/logger/buildLogMessage' | ||
import { LogData, LogLevel, Options, RequestInfo, StoreData } from '~/types' | ||
|
||
export async function logToTransports( | ||
level: LogLevel, | ||
request: RequestInfo, | ||
data: LogData, | ||
store: StoreData, | ||
options?: Options | ||
): Promise<void> { | ||
if (!options?.config?.transports || options.config.transports.length === 0) { | ||
return | ||
} | ||
|
||
const message = buildLogMessage(level, request, data, store, options, false) | ||
|
||
const promises = options.config.transports.map(transport => | ||
transport.log(level, message, { request, data, store }) | ||
) | ||
|
||
await Promise.all(promises) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { beforeEach, describe, expect, it, jest } from 'bun:test' | ||
|
||
import { logToTransports } from '~/transports' | ||
import { | ||
LogData, | ||
LogLevel, | ||
Options, | ||
RequestInfo, | ||
StoreData, | ||
Transport | ||
} from '~/types' | ||
|
||
describe('Custom Transports', () => { | ||
let mockTransport: Transport | ||
let options: Options | ||
|
||
beforeEach(() => { | ||
mockTransport = { | ||
log: jest.fn().mockResolvedValue(undefined) | ||
} | ||
options = { | ||
config: { | ||
transports: [mockTransport] | ||
} | ||
} | ||
}) | ||
|
||
it('should call the custom transport log function', async () => { | ||
const level: LogLevel = 'INFO' | ||
const request: RequestInfo = { | ||
url: '/test', | ||
method: 'GET', | ||
headers: { get: () => null } | ||
} | ||
const data: LogData = { status: 200 } | ||
const store: StoreData = { beforeTime: BigInt(0) } | ||
|
||
await logToTransports(level, request, data, store, options) | ||
|
||
expect(mockTransport.log).toHaveBeenCalledTimes(1) | ||
expect(mockTransport.log).toHaveBeenCalledWith(level, expect.any(String), { | ||
request, | ||
data, | ||
store | ||
}) | ||
}) | ||
|
||
it('should not call any transports if none are configured', async () => { | ||
const options: Options = { config: {} } | ||
const level: LogLevel = 'INFO' | ||
const request: RequestInfo = { | ||
url: '/test', | ||
method: 'GET', | ||
headers: { get: () => null } | ||
} | ||
const data: LogData = { status: 200 } | ||
const store: StoreData = { beforeTime: BigInt(0) } | ||
|
||
await logToTransports(level, request, data, store, options) | ||
|
||
expect(mockTransport.log).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should call multiple transports if configured', async () => { | ||
const secondMockTransport: Transport = { | ||
log: jest.fn().mockResolvedValue(undefined) | ||
} | ||
options.config!.transports!.push(secondMockTransport) | ||
|
||
const level: LogLevel = 'INFO' | ||
const request: RequestInfo = { | ||
url: '/test', | ||
method: 'GET', | ||
headers: { get: () => null } | ||
} | ||
const data: LogData = { status: 200 } | ||
const store: StoreData = { beforeTime: BigInt(0) } | ||
|
||
await logToTransports(level, request, data, store, options) | ||
|
||
expect(mockTransport.log).toHaveBeenCalledTimes(1) | ||
expect(secondMockTransport.log).toHaveBeenCalledTimes(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