Skip to content

Commit

Permalink
Merge pull request #11 from hmerritt/feat/log-improvements
Browse files Browse the repository at this point in the history
Feat / Log improvements
  • Loading branch information
hmerritt authored Jan 28, 2024
2 parents f631674 + 6e712cb commit 73ad7fd
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/lib/global/devTools.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { $global } from "./utils";
import { setGlobalValue } from "./utils";

// @TODO maybe remove these and add better dev tools.

Expand Down Expand Up @@ -41,6 +41,6 @@ export const getObjectOfEventListeners = () => {
};

export const injectDevTools = () => {
$global.getNumberOfEventListeners = getNumberOfEventListeners;
$global.getObjectOfEventListeners = getObjectOfEventListeners;
setGlobalValue("getNumberOfEventListeners", getNumberOfEventListeners);
setGlobalValue("getObjectOfEventListeners", getObjectOfEventListeners);
};
8 changes: 4 additions & 4 deletions src/lib/global/env.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { $global } from "./utils";
import { setGlobalValue } from "./utils";

/**
* Environment variables.
*
* Add all environment variables here to ensure type safety.
*/
export const env = {
export const env = Object.freeze({
// Core
appName: "App", // Optionally use `import.meta.env.VITE_NAME`
appVersion: import.meta.env.VITE_VERSION,
Expand All @@ -23,11 +23,11 @@ export const env = {
// Features
timerIncrement: import.meta.env.VITE_FEATURE_INCREMENT,
someOtherFeature: false
};
});

export type EnvObj = typeof env;
export type EnvKeys = keyof EnvObj;

export const injectEnv = () => {
$global.env = env;
setGlobalValue("env", env);
};
4 changes: 2 additions & 2 deletions src/lib/global/featureFlags.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type EnvKeys, env } from "./env";
import { $global } from "./utils";
import { setGlobalValue } from "./utils";

/**
* Returns `true` if the feature is enabled in `env` object.
Expand Down Expand Up @@ -32,7 +32,7 @@ export type FeatureOptions = {
export type FeatureFlags = EnvKeys;

export const injectFeature = () => {
$global.feature = feature;
setGlobalValue("feature", feature);
};

const isFalse = (value: unknown): value is false => {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/global/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ type FeatureFunction = (mode: FeatureFlags, options?: FeatureOptions) => boolean

declare global {
var log: LogFunction;
var logn: LogFunction;
var debug: LogFunction;
var debugn: LogFunction;
var logStore: LogStoreType;
var env: EnvObj;
var feature: FeatureFunction;
Expand All @@ -16,7 +18,9 @@ declare global {

interface Window {
log: LogFunction;
logn: LogFunction;
debug: LogFunction;
debugn: LogFunction;
logStore: LogStoreType;
env: EnvObj;
feature: FeatureFunction;
Expand Down
37 changes: 28 additions & 9 deletions src/lib/global/log.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import dayjs from "dayjs";

import { $global } from "./utils";
import { padChar } from "lib/strings";

import { $global, setGlobalValue } from "./utils";

// @TODO:
// export interface Logger {
// filter(level: string, tags: string[]): boolean;
Expand Down Expand Up @@ -82,8 +83,6 @@ const timestampString = (diff: chars, namespace?: string) => {
};

const _log = (namespace: string, logLevel: any, ...args: any[]) => {
if (import.meta.env.MODE === "production") return;

const timeElapsed = dayjs().diff($global.logStore.getTime(namespace), "millisecond");
const stringToLog = timestampString(timeElapsed, namespace);
$global.logStore.increment(namespace);
Expand All @@ -106,7 +105,7 @@ const _log = (namespace: string, logLevel: any, ...args: any[]) => {
};

/**
* log in development only (`NODE_ENV !== "production"`)
* log.
*
* Adds a timestamp and timediff to each log automatically.
*/
Expand All @@ -115,16 +114,36 @@ export const log = (logLevel: any, ...args: any[]) => {
};

/**
* Alias for `log`, plus namespaces logs to keep them separate.
* Namespaced `log`.
*
* @example debug("socket", "msg received") -> "[socket] msg recieved"
*/
export const debug = (namespace: string, logLevel: any, ...args: any[]) => {
export const logn = (namespace: string, logLevel: any, ...args: any[]) => {
_log(namespace, logLevel, ...args);
};

/**
* Log in development only (`NODE_ENV !== "production"`)
*/
export const debug = (logLevel: any, ...args: any[]) => {
if (env.isProduction) return;
log(logLevel, ...args);
};

/**
* Namespaced `debug`.
*
* @example debugn("socket", "msg received") -> "[socket] msg recieved"
*/
export const debugn = (namespace: string, logLevel: any, ...args: any[]) => {
if (env.isProduction) return;
logn(namespace, logLevel, ...args);
};

export const injectLog = () => {
$global.logStore = new LogStore();
$global.log = log;
$global.debug = debug;
setGlobalValue("logStore", new LogStore());
setGlobalValue("log", log);
setGlobalValue("logn", logn);
setGlobalValue("debug", debug);
setGlobalValue("debugn", debugn);
};
11 changes: 11 additions & 0 deletions src/lib/global/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,15 @@ export const getGlobal = () => {
}
};

/**
* Set immutable global variable.
*/
export const setGlobalValue = (key: string, value: any) => {
Object.defineProperty(getGlobal(), key, {
value: value,
configurable: false,
writable: false
});
};

export const $global = getGlobal();

0 comments on commit 73ad7fd

Please sign in to comment.