Skip to content

Commit c39805a

Browse files
More logging enhancements
1 parent 031847d commit c39805a

4 files changed

Lines changed: 48 additions & 31 deletions

File tree

src/app.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import fs from "fs";
55
import path from "node:path";
66
import swaggerUi from "swagger-ui-express";
77
import { routes } from "./routes";
8-
import { Log } from "./logging";
8+
import { Log, SetupLogging } from "./logging";
9+
10+
SetupLogging();
911

1012
const app = express();
1113

@@ -36,7 +38,7 @@ app.use((req: any, res: any) => {
3638
})
3739
});
3840

39-
Log({
41+
console.log({
4042
HostPort: port,
4143
ForwardingGitHubRequestsTo: process.env.GITHUB_PROXY ?? "Not forwarding",
4244
ForwardingGroupRequestsTo: process.env.SOURCE_PROXY ?? "Not forwarding"

src/logging.ts

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import appInsights from "applicationinsights";
1+
const appInsights = require("applicationinsights");
2+
3+
import { TelemetryType } from "applicationinsights/out/Declarations/Contracts";
24
import TelemetryClient from "applicationinsights/out/Library/TelemetryClient";
35

4-
export interface ILogger {
5-
Log(s:any):void
6-
LogError(s:any):void
6+
interface ILogger {
7+
Log(s:string):void
8+
LogError(ex: Error):void
79
}
810

911
class AiLogger implements ILogger {
@@ -12,37 +14,46 @@ class AiLogger implements ILogger {
1214
constructor(client: TelemetryClient) {
1315
this.client = client;
1416
}
15-
Log(s: any): void {
16-
throw new Error("Method not implemented.");
17+
Log(s: string): void {
18+
console.log(s);
19+
this.client.trackTrace({
20+
message: s
21+
});
1722
}
18-
LogError(s: any): void {
19-
throw new Error("Method not implemented.");
23+
LogError(ex: Error): void {
24+
console.log(ex);
25+
this.client.trackException({
26+
exception: ex
27+
});
2028
}
2129
}
2230

2331
class StdLogger implements ILogger {
24-
Log(s: any): void {
32+
Log(s: string): void {
2533
console.log(s);
2634
}
27-
LogError(s: any): void {
28-
console.log(s);
35+
LogError(ex: Error): void {
36+
console.log(ex);
2937
}
3038
}
3139

3240
let logger: ILogger = new StdLogger();
3341

34-
if(process.env.APPLICATIONINSIGHTS_CONNECTION_STRING) {
35-
appInsights.setup().start();
36-
37-
const telemetryClient = appInsights.defaultClient;
38-
39-
logger = new AiLogger(telemetryClient);
40-
}
41-
42-
export function Log(s:any) {
42+
export function Log(s:string) {
4343
logger.Log(s);
4444
}
4545

46-
export function LogError(s:any) {
47-
logger.LogError(s);
48-
}
46+
export function LogError(ex: Error) {
47+
logger.LogError(ex);
48+
}
49+
50+
export function SetupLogging() {
51+
if(process.env.APPLICATIONINSIGHTS_CONNECTION_STRING) {
52+
console.log("Using AppInsights Logger")
53+
appInsights.setup(process.env.APPLICATIONINSIGHTS_CONNECTION_STRING).start();
54+
55+
const telemetryClient = appInsights.defaultClient;
56+
57+
logger = new AiLogger(telemetryClient);
58+
}
59+
}

src/services/githubSync.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// REMEMBER TO REPLACE '_' with '-' for GitHub Names! 🤦‍♂️
22

3-
import { Log } from "../logging";
3+
import { Log, LogError } from "../logging";
44
import { AppConfig } from "./appConfig";
55
import { GitHubId, InstalledClient } from "./gitHubTypes";
66
import { SearchAllAsync } from "./ldapClient";
@@ -264,7 +264,7 @@ async function syncOrg(installedGitHubClient: InstalledClient, config: AppConfig
264264

265265
const orgConfig = orgConfigResponse.data;
266266

267-
Log(orgConfig);
267+
Log(JSON.stringify(orgConfig));
268268

269269
let currentMembers: GitHubId[] = [];
270270
if (orgConfig.OrganizationMembersGroup != undefined || orgConfig.OrganizationMembersGroup != null) {
@@ -323,7 +323,7 @@ export async function SyncOrg(installedGitHubClient: InstalledClient, config: Ap
323323
return await syncOrg(installedGitHubClient, config);
324324
}
325325
catch(error) {
326-
Log(error);
326+
LogError(error as any);
327327

328328
return {
329329
message: "Failed to sync org. Please check logs.",

src/services/ldapClient.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import ldap from "ldapjs";
33
import ldapEscape from "ldap-escape";
44
import axios from "axios";
55
import axiosRetry from "axios-retry";
6-
import { Log } from "../logging";
6+
import { Log, LogError } from "../logging";
77

88
const config = Config()
99

@@ -14,8 +14,12 @@ if(!process.env.SOURCE_PROXY) {
1414
url: [config.LDAP.Server]
1515
});
1616

17-
client.bind(config.LDAP.User, config.LDAP.Password, (err) => {
18-
Log(err);
17+
client.bind(config.LDAP.User, config.LDAP.Password, (err, result) => {
18+
if(err) {
19+
LogError(JSON.stringify(err) as any);
20+
}
21+
22+
console.log("Connected to LDAP Server");
1923
});
2024
}
2125
else {

0 commit comments

Comments
 (0)