next-axiom
Version:
Send WebVitals from your Next.js project to Axiom.
288 lines • 10.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.log = exports.Logger = exports.LogLevel = void 0;
exports.prettyPrint = prettyPrint;
const config_1 = require("./config");
const shared_1 = require("./shared");
const url = config_1.config.getLogsEndpoint();
const LOG_LEVEL = process.env.NEXT_PUBLIC_AXIOM_LOG_LEVEL || 'debug';
var LogLevel;
(function (LogLevel) {
LogLevel[LogLevel["debug"] = 0] = "debug";
LogLevel[LogLevel["info"] = 1] = "info";
LogLevel[LogLevel["warn"] = 2] = "warn";
LogLevel[LogLevel["error"] = 3] = "error";
LogLevel[LogLevel["off"] = 100] = "off";
})(LogLevel || (exports.LogLevel = LogLevel = {}));
class Logger {
initConfig;
logEvents = [];
throttledSendLogs = (0, shared_1.throttle)(this.sendLogs, 1000);
children = [];
logLevel = LogLevel.debug;
config = {
autoFlush: true,
source: 'frontend-log',
prettyPrint: prettyPrint,
};
constructor(initConfig = {}) {
this.initConfig = initConfig;
// check if user passed a log level, if not the default init value will be used as is.
if (this.initConfig.logLevel != undefined && this.initConfig.logLevel >= 0) {
this.logLevel = this.initConfig.logLevel;
}
else if (LOG_LEVEL) {
this.logLevel = LogLevel[LOG_LEVEL];
}
this.config = { ...this.config, ...initConfig };
}
debug = (message, args = {}) => {
this.log(LogLevel.debug, message, args);
};
info = (message, args = {}) => {
this.log(LogLevel.info, message, args);
};
warn = (message, args = {}) => {
this.log(LogLevel.warn, message, args);
};
error = (message, args = {}) => {
this.log(LogLevel.error, message, args);
};
with = (args) => {
const config = { ...this.config, args: { ...this.config.args, ...args } };
const child = new Logger(config);
this.children.push(child);
return child;
};
withRequest = (req) => {
return new Logger({ ...this.config, req: { ...this.config.req, ...req } });
};
_transformEvent = (level, message, args = {}) => {
const logEvent = {
level: LogLevel[level].toString(),
message,
_time: new Date(Date.now()).toISOString(),
source: this.config.source,
fields: this.config.args || {},
'@app': {
'next-axiom-version': config_1.Version,
},
};
// check if passed args is an object, if its not an object, add it to fields.args
if (args instanceof Error) {
logEvent.fields = {
...logEvent.fields,
message: args.message,
stack: args.stack,
name: args.name,
cause: args.cause,
};
}
else if (typeof args === 'object' && args !== null && Object.keys(args).length > 0) {
const parsedArgs = JSON.parse(JSON.stringify(args, jsonFriendlyErrorReplacer));
logEvent.fields = { ...logEvent.fields, ...parsedArgs };
}
else if (args && args.length) {
logEvent.fields = { ...logEvent.fields, args: args };
}
config_1.config.injectPlatformMetadata(logEvent, this.config.source);
if (this.config.req != null) {
logEvent.request = this.config.req;
if (logEvent.platform) {
logEvent.platform.route = this.config.req.path;
}
else if (logEvent.vercel) {
logEvent.vercel.route = this.config.req.path;
}
}
return logEvent;
};
logHttpRequest(level, message, request, args) {
const logEvent = this._transformEvent(level, message, args);
logEvent.request = request;
this.logEvents.push(logEvent);
if (this.config.autoFlush) {
this.throttledSendLogs();
}
}
middleware(request, config) {
const req = {
ip: request.ip,
region: request.geo?.region,
method: request.method,
host: request.nextUrl.hostname,
path: request.nextUrl.pathname,
scheme: request.nextUrl.protocol.split(':')[0],
referer: request.headers.get('Referer'),
userAgent: request.headers.get('user-agent'),
};
const message = `${request.method} ${request.nextUrl.pathname}`;
if (config?.logRequestDetails) {
return (0, shared_1.requestToJSON)(request).then((details) => {
const newReq = {
...req,
details: Array.isArray(config.logRequestDetails)
? Object.fromEntries(Object.entries(details).filter(([key]) => config.logRequestDetails.includes(key)))
: details,
};
return this.logHttpRequest(LogLevel.info, message, newReq, {});
});
}
return this.logHttpRequest(LogLevel.info, message, req, {});
}
log = (level, message, args = {}) => {
if (level < this.logLevel) {
return;
}
const logEvent = this._transformEvent(level, message, args);
this.logEvents.push(logEvent);
if (this.config.autoFlush) {
this.throttledSendLogs();
}
};
attachResponseStatus = (statusCode) => {
this.logEvents = this.logEvents.map((log) => {
if (log.request) {
log.request.statusCode = statusCode;
}
return log;
});
};
async sendLogs() {
if (!this.logEvents.length) {
return;
}
// To send logs over the network, we need one of:
//
// - Axiom URL and Axiom dataset and Axiom token
// - Axiom Vercel ingest URL
// - Custom endpoint
//
// We fall back to printing to console to avoid network errors in
// development environments.
if (!config_1.config.isEnvVarsSet()) {
this.logEvents.forEach((ev) => (this.config.prettyPrint ? this.config.prettyPrint(ev) : prettyPrint(ev)));
this.logEvents = [];
return;
}
const method = 'POST';
const keepalive = true;
const body = JSON.stringify(this.logEvents);
// clear pending logs
this.logEvents = [];
const headers = {
'Content-Type': 'application/json',
'User-Agent': 'next-axiom/v' + config_1.Version,
};
if (config_1.config.token) {
headers['Authorization'] = `Bearer ${config_1.config.token}`;
}
const reqOptions = { body, method, keepalive, headers };
function sendFallback() {
// Do not leak network errors; does not affect the running app
return fetch(url, reqOptions).catch(console.error);
}
try {
if (typeof fetch === 'undefined') {
const fetch = await require('whatwg-fetch');
return fetch(url, reqOptions).catch(console.error);
}
else if (config_1.isBrowser && config_1.isVercelIntegration && navigator.sendBeacon) {
// sendBeacon fails if message size is greater than 64kb, so
// we fall back to fetch.
// Navigator has to be bound to ensure it does not error in some browsers
// https://xgwang.me/posts/you-may-not-know-beacon/#it-may-throw-error%2C-be-sure-to-catch
try {
if (!navigator.sendBeacon.bind(navigator)(url, body)) {
return sendFallback();
}
}
catch (error) {
return sendFallback();
}
}
else {
return sendFallback();
}
}
catch (e) {
console.warn(`Failed to send logs to Axiom: ${e}`);
// put the log events back in the queue
this.logEvents = [...this.logEvents, JSON.parse(body)];
}
}
flush = async () => {
await Promise.all([this.sendLogs(), ...this.children.map((c) => c.flush())]);
};
}
exports.Logger = Logger;
exports.log = new Logger({});
const levelColors = {
info: {
terminal: '32',
browser: 'lightgreen',
},
debug: {
terminal: '36',
browser: 'lightblue',
},
warn: {
terminal: '33',
browser: 'yellow',
},
error: {
terminal: '31',
browser: 'red',
},
};
function prettyPrint(ev) {
const hasFields = Object.keys(ev.fields).length > 0;
// check whether pretty print is disabled
if (shared_1.isNoPrettyPrint) {
let msg = `${ev.level} - ${ev.message}`;
if (hasFields) {
msg += ' ' + JSON.stringify(ev.fields);
}
console.log(msg);
return;
}
// print indented message, instead of [object]
// We use the %o modifier instead of JSON.stringify because stringify will print the
// object as normal text, it loses all the functionality the browser gives for viewing
// objects in the console, such as expanding and collapsing the object.
let msgString = '';
let args = [ev.level, ev.message];
if (config_1.isBrowser) {
msgString = '%c%s - %s';
args = [`color: ${levelColors[ev.level].browser};`, ...args];
}
else {
msgString = `\x1b[${levelColors[ev.level].terminal}m%s\x1b[0m - %s`;
}
// we check if the fields object is not empty, otherwise its printed as <empty string>
// or just "".
if (hasFields) {
msgString += ' %o';
args.push(ev.fields);
}
if (ev.request) {
msgString += ' %o';
args.push(ev.request);
}
console.log.apply(console, [msgString, ...args]);
}
function jsonFriendlyErrorReplacer(key, value) {
if (value instanceof Error) {
return {
// Pull all enumerable properties, supporting properties on custom Errors
...value,
// Explicitly pull Error's non-enumerable properties
name: value.name,
message: value.message,
stack: value.stack,
cause: jsonFriendlyErrorReplacer('', value.cause),
};
}
return value;
}
//# sourceMappingURL=logger.js.map