Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove azure/logger #876

Merged
merged 4 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions Library/InternalAzureLogger.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import fs = require("fs");
import os = require("os");
import path = require("path");
import { AzureLogger, setLogLevel, createClientLogger } from "@azure/logger";
import FileSystemHelper = require("./FileSystemHelper");


class InternalAzureLogger {

private static _instance: InternalAzureLogger;
public logger: AzureLogger;
public maxHistory: number;
public maxSizeBytes: number;

Expand All @@ -19,18 +17,18 @@ class InternalAzureLogger {
public _logFileName: string;
private _fileFullPath: string;
private _backUpNameFormat: string;
private _logToFile = false;
private _logToConsole = true;


constructor() {
setLogLevel("verbose"); // Verbose so we can control log level in our side
let logDestination = process.env.APPLICATIONINSIGHTS_LOG_DESTINATION; // destination can be one of file, console or file+console
let logToFile = false;
let logToConsole = true;
if (logDestination == "file+console") {
logToFile = true;
this._logToFile = true;
}
if (logDestination == "file") {
logToFile = true;
logToConsole = false;
this._logToFile = true;
this._logToConsole = false;
}
this.maxSizeBytes = 50000;
this.maxHistory = 1;
Expand All @@ -52,24 +50,34 @@ class InternalAzureLogger {
this._fileFullPath = path.join(this._tempDir, this._logFileName);
this._backUpNameFormat = "." + this._logFileName; // {currentime}.applicationinsights.log

// Override AzureLogger to also enable logs to be stored in disk
AzureLogger.log = (...args) => {
if (logToFile) {
this._storeToDisk(args);
}
if (logToConsole) {
console.log(...args);
}
};
this.logger = createClientLogger('ApplicationInsights');
if (logToFile) {
if (this._logToFile) {
if (!InternalAzureLogger._fileCleanupTimer) {
InternalAzureLogger._fileCleanupTimer = setInterval(() => { this._fileCleanupTask(); }, this._cleanupTimeOut);
InternalAzureLogger._fileCleanupTimer.unref();
}
}
}

public info(message?: any, ...optionalParams: any[]) {
let args = message ? [message, ...optionalParams] : optionalParams;
if (this._logToFile) {
this._storeToDisk(args);
}
if (this._logToConsole) {
console.info(...args);
}
}

public warning(message?: any, ...optionalParams: any[]) {
let args = message ? [message, ...optionalParams] : optionalParams;
if (this._logToFile) {
this._storeToDisk(args);
}
if (this._logToConsole) {
console.warn(...args);
}
}

static getInstance() {
if (!InternalAzureLogger._instance) {
InternalAzureLogger._instance = new InternalAzureLogger();
Expand Down
4 changes: 2 additions & 2 deletions Library/Logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ class Logging {

public static info(message?: any, ...optionalParams: any[]) {
if (this.enableDebug) {
InternalAzureLogger.getInstance().logger.info(this.TAG + message, optionalParams);
InternalAzureLogger.getInstance().info(this.TAG + message, optionalParams);
}
}

public static warn(message?: any, ...optionalParams: any[]) {
if (!this.disableWarnings) {
InternalAzureLogger.getInstance().logger.warning(this.TAG + message, optionalParams);
InternalAzureLogger.getInstance().warning(this.TAG + message, optionalParams);
}
}
}
Expand Down
26 changes: 16 additions & 10 deletions Tests/Library/Config.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert = require("assert");
import sinon = require("sinon");
var http = require("http");
var https = require("https");

import Config = require("../../Library/Config");
import Constants = require("../../Declarations/Constants");

Expand All @@ -14,11 +13,21 @@ describe("Library/Config", () => {

var iKey = "1aa11111-bbbb-1ccc-8ddd-eeeeffff3333";
var appVer = "appVer";

var sandbox: sinon.SinonSandbox;

before(() => {
sandbox = sinon.sandbox.create();
});

beforeEach(() => {
JsonConfig["_jsonConfig"] = undefined;
});

afterEach(() => {
sandbox.restore();
});

describe("#constructor", () => {
describe("connection string && API && environment variable prioritization", () => {
it ("connection string set via in code setup", () => {
Expand Down Expand Up @@ -148,24 +157,21 @@ describe("Library/Config", () => {
});

it("instrumentation key validation-valid key passed", () => {
var warnStub = sinon.stub(console, "warn");
var warnStub = sandbox.stub(console, "warn");
var config = new Config("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333");
assert.ok(warnStub.notCalled, "warning was not raised");
warnStub.restore();
assert.ok(warnStub.calledOnce, "warning was not raised due to ikey checking, warning is called once since config json path is not configured");
});

it("instrumentation key validation-invalid key passed", () => {
var warnStub = sinon.stub(console, "warn");
var warnStub = sandbox.stub(console, "warn");
var config = new Config("1aa11111bbbb1ccc8dddeeeeffff3333");
assert.ok(warnStub.calledOn, "warning was raised");
warnStub.restore();
assert.ok(warnStub.calledTwice, "warning was raised once due to ikey checking, the second call is caused by config json path is not configured");
});

it("instrumentation key validation-invalid key passed", () => {
var warnStub = sinon.stub(console, "warn");
var warnStub = sandbox.stub(console, "warn");
var config = new Config("abc");
assert.ok(warnStub.calledOn, "warning was raised");
warnStub.restore();
assert.ok(warnStub.calledTwice, "warning was raised due to ikey checking, the second call is caused by config json path is not configured");
});

});
Expand Down
12 changes: 6 additions & 6 deletions Tests/Library/Logging.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("Library/Logging", () => {
var originalEnv = process.env;
process.env = env1;
Logging.enableDebug = true;
var consoleStub = sandbox.stub(console, "log");
var consoleStub = sandbox.stub(console, "info");
Logging.info("test");
process.env = originalEnv;
assert.ok(consoleStub.called);
Expand All @@ -36,7 +36,7 @@ describe("Library/Logging", () => {
var originalEnv = process.env;
process.env = env1;
Logging.enableDebug = true;
var consoleStub = sandbox.stub(console, "log");
var consoleStub = sandbox.stub(console, "info");
Logging.info("test");
process.env = originalEnv;
assert.ok(consoleStub.notCalled);
Expand All @@ -47,7 +47,7 @@ describe("Library/Logging", () => {
it("should do nothing if disabled", () => {
var originalSetting = Logging.disableWarnings;
Logging.enableDebug = false;
var infoStub = sandbox.stub(InternalAzureLogger.getInstance().logger, "info");
var infoStub = sandbox.stub(InternalAzureLogger.getInstance(), "info");
Logging.info("test");
assert.ok(infoStub.notCalled);
Logging.enableDebug = originalSetting;
Expand All @@ -56,7 +56,7 @@ describe("Library/Logging", () => {
it("should log 'info' if called", () => {
var originalSetting = Logging.enableDebug;
Logging.enableDebug = true;
var infoStub = sandbox.stub(InternalAzureLogger.getInstance().logger, "info");
var infoStub = sandbox.stub(InternalAzureLogger.getInstance(), "info");
Logging.info("test");
assert.ok(infoStub.calledOnce);
Logging.enableDebug = originalSetting;
Expand All @@ -67,7 +67,7 @@ describe("Library/Logging", () => {
it("should do nothing if disabled", () => {
var originalSetting = Logging.disableWarnings;
Logging.disableWarnings = true
var warnStub = sandbox.stub(InternalAzureLogger.getInstance().logger, "warning");
var warnStub = sandbox.stub(InternalAzureLogger.getInstance(), "warning");
Logging.warn("test");
assert.ok(warnStub.notCalled);
Logging.enableDebug = originalSetting;
Expand All @@ -76,7 +76,7 @@ describe("Library/Logging", () => {
it("should log 'warn' if enabled", () => {
var originalSetting = Logging.disableWarnings;
Logging.disableWarnings = false;
var warnStub = sandbox.stub(InternalAzureLogger.getInstance().logger, "warning");
var warnStub = sandbox.stub(InternalAzureLogger.getInstance(), "warning");
Logging.warn("test");
assert.ok(warnStub.calledOnce);
Logging.enableDebug = originalSetting;
Expand Down
4 changes: 2 additions & 2 deletions Tests/applicationInsights.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ describe("ApplicationInsights", () => {
it("auto-collection is initialized by default", () => {
AppInsights.setup("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333").start();

//assert.ok(Console.INSTANCE.isInitialized());
setTimeout(() => {
//assert.ok(Console.INSTANCE.isInitialized());
assert.ok(Exceptions.INSTANCE.isInitialized());
assert.ok(Performance.INSTANCE.isInitialized());
assert.ok(HttpRequests.INSTANCE.isInitialized());
assert.ok(HttpRequests.INSTANCE.isAutoCorrelating());
assert.ok(HttpDependencies.INSTANCE.isInitialized());
}, 1000)
}, 5000);
});

it("auto-collection is not initialized if disabled before 'start'", () => {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
},
"dependencies": {
"@azure/core-http": "^2.2.2",
"@azure/logger": "^1.0.1",
"@opentelemetry/api": "^1.0.3",
"@opentelemetry/core": "^0.23.0",
"@opentelemetry/semantic-conventions": "^0.24.0",
Expand Down