Skip to content

Commit

Permalink
Move logging to singleton class, add env configs, and log some initia…
Browse files Browse the repository at this point in the history
…l events
  • Loading branch information
daniellrgn committed Jan 28, 2025
1 parent 6db44d3 commit 129e96e
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 0 deletions.
5 changes: 5 additions & 0 deletions default.env
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ COMPOSE_FILE=docker-compose.yaml:docker-compose.static-ingress.yaml
# Enable to use development overrides
# COMPOSE_FILE=docker-compose.yaml:docker-compose.dev.yaml

# Logging configuration
#VITE_DEPLOYMENT_TYPE=
#VITE_LOG_URL=
#VITE_SYSTEM_URL=

# SHL Server API endpoint url
VITE_API_BASE=

Expand Down
3 changes: 3 additions & 0 deletions src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ interface ImportMetaEnv {
readonly VITE_EPIC_CLIENT_ID: string
readonly VITE_CERNER_CLIENT_ID: string
readonly VITE_API_BASE: string
readonly VITE_LOG_URL: string
readonly VITE_DEPLOYMENT_TYPE: "dev" | "test" | "demo" | "stage" | "prod"
readonly VITE_SYSTEM_URL: string
readonly VITE_VIEWER_BASE: string
readonly VITE_INTERMEDIATE_FHIR_SERVER_BASE: string
readonly VITE_SOF_CLIENT_ID: string
Expand Down
4 changes: 4 additions & 0 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import { dev } from '$app/environment';
import { toMilliseconds } from '$lib/util';

export const LOG_URL = import.meta.env.VITE_LOG_URL;
export const DEPLOYMENT_TYPE = import.meta.env.VITE_DEPLOYMENT_TYPE;
export const SYSTEM_URL = import.meta.env.VITE_SYSTEM_URL;

export const API_BASE = import.meta.env.VITE_API_BASE;

export const INTERMEDIATE_FHIR_SERVER_BASE = import.meta.env.VITE_INTERMEDIATE_FHIR_SERVER_BASE;
Expand Down
84 changes: 84 additions & 0 deletions src/lib/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { LOG_URL, DEPLOYMENT_TYPE, SYSTEM_URL } from "./config";
import { dev } from "$app/environment";

interface LogMessage {
"message": string;
"tags"?: string[];
"level"?: "INFO" | "ERROR" | "WARN" | "DEBUG";
"subject"?: string;
"user"?: string;
"name"?: string;
"deployment"?: "dev" | "test" | "demo" | "stage" | "prod";
"system-type"?: string;
"system-url"?: string;
"session-id"?: string;
}
export function log(content: LogMessage|string, dest?: string) {
if (typeof content === "string") {
content = {
message: content
};
}
Logger.Instance.log(content, dest);
}

export class Logger {
private static _instance: Logger;
user_id: string;
session_id: string;
dest: string;

private constructor(user_id: string = "", session_id: string = "") {
this.dest = LOG_URL || "";
this.user_id = user_id;
this.session_id = session_id;
}

public static get Instance(): Logger {
return this._instance || (this._instance = new this());
}

public setUserId(user_id: string): void {
this.user_id = user_id;
}

public setSessionId(session_id: string): void {
this.session_id = session_id;
}

public log(content: LogMessage, dest?: string): void {
let logURL = dest ?? this.dest;
if (!logURL) {
console.log(JSON.stringify(content));
return;
}
if (!URL.canParse(logURL)) {
console.warn('Invalid log destination: ' + logURL);
console.log(JSON.stringify(content));
return;
}

let defaults = {
"level": "INFO",
"user": this.user_id,
"name": "shl-creator",
"deployment": DEPLOYMENT_TYPE || dev ? "dev" : "prod",
"system-type": "web",
"system-url": SYSTEM_URL || window.location.href,
"session-id": this.session_id,
};

let logMessage = {
...defaults,
...content,
};

fetch(logURL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(logMessage),
});
}
}
6 changes: 6 additions & 0 deletions src/lib/sofClient.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import FHIR from 'fhirclient';
import { SOF_PATIENT_RESOURCES, SOF_RESOURCES, LOGOUT_URL, POST_LOGOUT_REDIRECT_URI } from './config.ts';
import { Logger, log } from './logger';

const patientResourceScope = SOF_PATIENT_RESOURCES.map(resourceType => `patient/${resourceType}.read`);
const resourceScope = patientResourceScope.join(" ");
Expand All @@ -18,6 +19,8 @@ export class SOFClient {
// Initialize FHIR client
this.client = await FHIR.oauth2.init(this.configuration);
this.patientId = this.getKeyCloakUserID();
Logger.Instance.setUserId(this.patientId);
log({ message: `FHIR client initialized for user ${this.patientId}` });
} catch (error) {
console.error('Error initializing FHIR client:', error);
}
Expand All @@ -42,7 +45,10 @@ export class SOFClient {
this.reset();
if (logout_url !== "") {
window.location.href = logout_url;
log({ message: `User ${this.patientId} logged out` });
Logger.Instance.setUserId("");
} else {
log({ message: `Failed to log out user ${this.patientId}: empty logout URL`, level: "ERROR" });
throw Error("Empty logout URL");
}
}
Expand Down
1 change: 1 addition & 0 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import { log } from '$lib/logger';
import { setContext, onMount } from 'svelte';
import { writable } from 'svelte/store';
import {
Expand Down
6 changes: 6 additions & 0 deletions src/routes/share/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<script lang="ts">
import HealthLinkLTT from '$lib/HealthLinkLTT.svelte';
import AddFileLTT from '$lib/AddFileLTT.svelte';
import { onMount } from 'svelte';
import { log } from '$lib/logger';
let shlReady = false;
function updateReady(ready: boolean) {
shlReady = ready;
}
onMount(() => {
log("Share page loaded" );
})
</script>

<svelte:head>
Expand Down

0 comments on commit 129e96e

Please sign in to comment.