add betterstack logs
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { isBot } from '@/bots';
|
||||
import { logInfo } from '@/utils/logger';
|
||||
import { getClientIp, parseIp } from '@/utils/parseIp';
|
||||
import { getReferrerWithQuery, parseReferrer } from '@/utils/parseReferrer';
|
||||
import { isUserAgentSet, parseUserAgent } from '@/utils/parseUserAgent';
|
||||
@@ -188,17 +189,17 @@ export async function postEvent(
|
||||
!sessionEndJobCurrentDeviceId && !sessionEndJobPreviousDeviceId;
|
||||
|
||||
if (sessionEndJobCurrentDeviceId && !sessionEndJobPreviousDeviceId) {
|
||||
console.log('found session current');
|
||||
logInfo('found session current');
|
||||
deviceId = currentDeviceId;
|
||||
const diff = Date.now() - sessionEndJobCurrentDeviceId.timestamp;
|
||||
sessionEndJobCurrentDeviceId.changeDelay(diff + SESSION_END_TIMEOUT);
|
||||
} else if (!sessionEndJobCurrentDeviceId && sessionEndJobPreviousDeviceId) {
|
||||
console.log('found session previous');
|
||||
logInfo('found session previous');
|
||||
deviceId = previousDeviceId;
|
||||
const diff = Date.now() - sessionEndJobPreviousDeviceId.timestamp;
|
||||
sessionEndJobPreviousDeviceId.changeDelay(diff + SESSION_END_TIMEOUT);
|
||||
} else {
|
||||
console.log('new session with current');
|
||||
logInfo('new session with current');
|
||||
deviceId = currentDeviceId;
|
||||
// Queue session end
|
||||
eventsQueue.add(
|
||||
@@ -216,6 +217,20 @@ export async function postEvent(
|
||||
);
|
||||
}
|
||||
|
||||
logInfo('incoming event', {
|
||||
ip,
|
||||
origin,
|
||||
ua,
|
||||
uaInfo,
|
||||
referrer,
|
||||
profileId,
|
||||
projectId,
|
||||
deviceId,
|
||||
bot,
|
||||
geo,
|
||||
events,
|
||||
});
|
||||
|
||||
const payload: Omit<IServiceCreateEventPayload, 'id'> = {
|
||||
name: body.name,
|
||||
deviceId,
|
||||
@@ -260,9 +275,10 @@ export async function postEvent(
|
||||
|
||||
if (payload.name === 'screen_view') {
|
||||
if (duration < 0) {
|
||||
console.log('--- DURATION IS NEGATIVE ---');
|
||||
console.log('prevEvent', JSON.stringify(prevEvent));
|
||||
console.log('current', JSON.stringify(payload));
|
||||
logInfo('duration is wrong', {
|
||||
prevEvent,
|
||||
payload,
|
||||
});
|
||||
}
|
||||
await job.updateData({
|
||||
type: 'createEvent',
|
||||
@@ -291,6 +307,7 @@ export async function postEvent(
|
||||
options.jobId = `event:${projectId}:${deviceId}:${Date.now()}`;
|
||||
}
|
||||
|
||||
logInfo('queue event', payload);
|
||||
// Queue current event
|
||||
eventsQueue.add(
|
||||
'event',
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import cors from '@fastify/cors';
|
||||
import Fastify from 'fastify';
|
||||
import pino from 'pino';
|
||||
|
||||
import { redisPub } from '@mixan/redis';
|
||||
|
||||
@@ -8,6 +7,7 @@ import eventRouter from './routes/event.router';
|
||||
import liveRouter from './routes/live.router';
|
||||
import miscRouter from './routes/misc.router';
|
||||
import profileRouter from './routes/profile.router';
|
||||
import { logger, logInfo } from './utils/logger';
|
||||
|
||||
declare module 'fastify' {
|
||||
interface FastifyRequest {
|
||||
@@ -18,11 +18,10 @@ declare module 'fastify' {
|
||||
const port = parseInt(process.env.API_PORT || '3000', 10);
|
||||
|
||||
const startServer = async () => {
|
||||
logInfo('Starting server');
|
||||
try {
|
||||
const fastify = Fastify({
|
||||
logger: pino({
|
||||
level: 'info',
|
||||
}),
|
||||
logger: logger,
|
||||
});
|
||||
|
||||
fastify.register(cors, {
|
||||
@@ -50,12 +49,12 @@ const startServer = async () => {
|
||||
// })
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
for (const signal of ['SIGINT', 'SIGTERM']) {
|
||||
process.on(signal, () =>
|
||||
process.on(signal, (err) => {
|
||||
logger.fatal(err, `uncaught exception detected ${signal}`);
|
||||
fastify.close().then((err) => {
|
||||
console.log(`close application on ${signal}`);
|
||||
process.exit(err ? 1 : 0);
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,9 +70,4 @@ const startServer = async () => {
|
||||
}
|
||||
};
|
||||
|
||||
process.on('unhandledRejection', (e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
startServer();
|
||||
|
||||
19
apps/sdk-api/src/utils/logger.ts
Normal file
19
apps/sdk-api/src/utils/logger.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import pino from 'pino';
|
||||
|
||||
const transport = pino.transport({
|
||||
targets: [
|
||||
{
|
||||
target: '@logtail/pino',
|
||||
options: { sourceToken: process.env.BETTERSTACK_TOKEN },
|
||||
},
|
||||
{
|
||||
target: 'pino-pretty',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
export const logger = pino(transport);
|
||||
|
||||
export function logInfo(msg: string, obj?: unknown) {
|
||||
logger.info(obj, msg);
|
||||
}
|
||||
Reference in New Issue
Block a user