Acelogger is a light and powerful logger with tracing and metrics logging. It is inspired by opentelemetry-js.
opentelemetry, opentracing and other libraries, are hard to understand and use.
-
too many concepts. TracerProvider, Tracer, SpanProcessor, Span, Logger, Event, Metric, MetricProvider etc.
-
large code size
- @opentelemetry/tracing minimized 39.9kb
- @opentelemetry/api minimized 13.8kb
- @opentelemetry/web minimized 46.1kb
- acelogger minimized < 10kb
-
bad design
- circulation dependencies
- do not support tree shaking
The logging, tracing and metrics will generate events which logged by logger, event have different alert level and event type.
- tracer is just the factory class for span.
- logger use tracer to create span and span context.
- logger can create, record and export events.
- define exporter with alert level, includes: debug, info, warn, error, fatal
- exporter will sample the events
- event type includes:
- log event, for message logging
- store, count and timing events, for metric reporting
- span start and span end events, for tracing
$ npm install --save acelogger
import ace, {
ConsoleExporterWeb,
LogLevel,
CanonicalCode,
isMetricEvent,
} from 'acelogger';
ace.logger.setAttributes({
os: 'MacOS',
osVersion: '13.0',
});
class MetricExporterWeb implements LoggerEventExporter {
export(evts: LoggerEvent[]) {
const metricEvents = evts.filter(isMetricEvent);
console.log(metricEvents);
}
}
// set event exporter, for mesage logging
ace.setExporter(LogLevel.Debug, new ConsoleExporterWeb());
// set event exporter, for metric reportings
ace.setExporter(LogLevel.Debug, new MetricExporterWeb());
// log info level message
ace.logger.info('test info');
ace.logger.storeMetrics({
metrics: {
memoryUsage: 0.1,
cpuUsage: 0.5,
},
});
ace.logger.event('button_click');
// creat a span logger
const spanLogger = ace.logger.startSpan('first.span', {
parent: ace.tracer.createSpanContext(),
});
spanLogger.setAttributes({
path: '/path/to',
});
spanLogger.info('test info');
spanLogger.event('button_click');
spanLogger.endSpan({
status: CanonicalCode.OK,
});
when logging in module, you can customize a scoped logger.
import globaleAce, {
SimpleManager,
LogLevel,
ConsoleExporterWeb,
} from 'acelogger';
const manager = new SimpleManager();
manager.logger.setAttributes({
app: globaleAce.logger.getAttributes().app,
appVersion: globaleAce.logger.getAttributes().appVersion,
lib: 'my-module@0.0.1',
});
// init logger exporter
manager.setExporter(LogLevel.Debug, new ConsoleExporterWeb());
manager.setBufferSize(0);
export { manager as ace };
you can customize a exporter
import ace, { TraceFlags, SimpleManager, LogLevel, ConsoleExporterWeb, isCountEvent } from 'acelogger';
// init logger exporter
manager.setExporter(LogLevel.Debug, {
export(
events: LoggerEvent[],
resultCallback: (result: ExportResult) => void
): void {
for (const evt of events) {
if (evt.traceFlags === TraceFlags.SAMPLED && !isSimpled(evt)) {
return;
}
if (evt.metrics) {
logMetric(evt);
} else if (isCountEvent(evt)) {
logCount(evt)
}
logToFile(evt);
}
};
});
- feat: remove duration infomation in endSpan message
- feat: set logStart default is true
- feat: add IDGenerator api
- fix: default span id start with 0
- feat: add userStartTime to span start event
- refactor: new exporter api (break change)
- refactor: add some util for event name and metric name
- feat: enhancement default traceId and spanId
- feat: support set flush ready flag
- feat: remove setBufferSize
- feat: chage samplingRatio to samplingRate
- feat: SimpleManager support customize flush delay time
- feat: add samplingRatio for event and disable log start event by default
- feat: startSpan add logStart param to disable start span event
- fix: change __debug to __ace_debug
- feat: support data option for Logger.startSpan
- fix: logger.error and fatal not support string as message
- optimize: async flush events
- optimize: console expoter default only print warn, error, fatal logs
- fix: use strict rule for function type
- fix: storeMetrics support status
- refactor: LoggerEvent structure changed
- refactor: remove isCountEvent
- refactor: move setExporter & setBufferSize to Manager
- feat: move spanId and traceId to LoggerEvent.data
- update README.md
- remove api count, timing, store
- add storeMetrics, event api
- remove some default attributes of LoggerEvent
- add fatal for Logger
- Logger.count only record 1 time
- LoggerEvent add metrics property and data property will without metrics
- add traceFlags for LoggerEvent, which can get sampled flag
- change endSpan message format, now have duration and customized message
- refactor isMetricEvent
- break change SimpleLogger api, to reduce duplicate logs
- remove unsafe dependencies
- refactor: new Manager api
- fix: build with no files
- fix: console logger name is undefined
- remove span methods
- new singleton api
- fix opentelemetry-api do not support tree shaking problem
- first version with logger and tracer