Node.jsのMomentoによる観測可能性
ロギング
すべてのMomento SDKの目標は、開発者がMomentoのログ出力を、アプリケーションのログの残りの部分に使用しているのと同じ宛先に向けることができるようにすることです。したがって、特定のプログラミング言語の一般的なロギングフレームワークのすべてと互換性があることを目指しています。
node.jsで利用可能なロギング・ライブラリは数多くあります。人気のあるものには、以下のようなものがあります:
Momento がこれらのライブラリ (そしてそれ以上!) と互換性があることを保証するために、私たちは軽量なロギングファサードを提供しています。これを使うには、 MomentoLoggerFactory
インターフェイスと MomentoLogger
インターフェイスを実装するだけです:
export interface MomentoLogger {
error(msg: string, ...args: unknown[]): void;
warn(msg: string, ...args: unknown[]): void;
info(msg: string, ...args: unknown[]): void;
debug(msg: string, ...args: unknown[]): void;
trace(msg: string, ...args: unknown[]): void;
}
export interface MomentoLoggerFactory {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getLogger(loggerName: string | any): MomentoLogger;
}
あなたの実装は、選択したロギングライブラリの薄いラッパーになるでしょう。私たちは、pino
ロガーを使用する完全で動作する実装例を提供しています。ソースコードはこちらを参照してください。
MomentoLoggerFactoryのインスタンスを作成するときに、特定のロガーレベルを指定することができます。その後、指定したロガーレベルを使用する
MomentoLogger` インスタンスをそのファクトリーから取得できます。
// Setting the level to ERROR means you will see error messages but
// no trace, info, debug, or warning messages.
const errorLoggerFactory = new DefaultMomentoLoggerFactory(DefaultMomentoLoggerLevel.ERROR);
const errorLogger = errorLoggerFactory.getLogger('momento-error-logger');
errorLogger.error('error in the code!');
// Setting the level to DEBUG means you will see error, info, debug,
// and warning messages but no trace messages.
const debugLoggerFactory = new DefaultMomentoLoggerFactory(DefaultMomentoLoggerLevel.DEBUG);
const debugLogger = debugLoggerFactory.getLogger('momento-debug-logger');
debugLogger.debug('helpful debugging message');
MomentoLoggerFactoryと
MomentoLogger` を定義したら、最後に Momento クライアントが好みのロガーを使うように設定します:
return new CacheClient({
configuration: Configurations.Laptop.v1(
new PinoMomentoLoggerFactory({
transport: {
target: 'pino-pretty',
options: {
colorize: true,
},
},
})
),
credentialProvider: CredentialProvider.fromEnvironmentVariable({environmentVariableName: 'MOMENTO_API_KEY'}),
defaultTtlSeconds: 60,
});
そうすると、Momentoからのログメッセージがpinoのロギング環境を通して表示されるはずです。この場合、以下のようなログメッセージが表示されるはずです:
[1685649962168] INFO (CacheClient/4386 on mycomputer.local): Creating Momento CacheClient
[1685649962168] INFO (ControlClient/4386 on mycomputer.local): Creating cache: test-cache