/
statusLogger.ts
42 lines (36 loc) · 1.49 KB
/
statusLogger.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import * as os from "os";
import { FileWriter } from "./writers/fileWriter";
import { IAgentLogger, IStatusContract, LOGGER_LANGUAGE } from "../types";
import { APPLICATION_INSIGHTS_OPENTELEMETRY_VERSION } from "../../types";
export class StatusLogger {
private _instrumentationKey: string;
private _language: string;
private _sdkVersion: string;
private _processId: string;
private _machineName: string;
private _agentLogger: IAgentLogger;
constructor(instrumentationKey: string, agentLogger: IAgentLogger = console) {
this._agentLogger = agentLogger;
this._instrumentationKey = instrumentationKey;
this._language = LOGGER_LANGUAGE;
this._sdkVersion = APPLICATION_INSIGHTS_OPENTELEMETRY_VERSION;
this._machineName = os.hostname();
this._processId = String(process.pid);
}
public logStatus(status: IStatusContract, cb?: (err: Error) => void) {
this._addCommonProperties(status);
if (typeof cb === "function" && this._agentLogger instanceof FileWriter) {
this._agentLogger.callback = cb;
}
this._agentLogger.log(status);
}
private _addCommonProperties(status: IStatusContract) {
status.AppType = this._language;
status.MachineName = this._machineName;
status.PID = this._processId;
status.SdkVersion = this._sdkVersion;
status.Ikey = this._instrumentationKey;
}
}