Skip to content

Commit

Permalink
fix: better log methods
Browse files Browse the repository at this point in the history
  • Loading branch information
BearToCode committed Dec 21, 2023
1 parent 210a87d commit 1569c09
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export function cache(opts: CacheOptions) {

const cacheConfig =
queryCacheArgs ?? opts.models?.[model] ?? opts.default ?? DefaultCacheConfig;
logger.debug(`Cache config for model ${model} with operation ${operation}: `, cacheConfig);

if (cacheConfig.get) {
const cached = await db.get<CachedResult>({ model, operation, args });
Expand All @@ -77,8 +78,11 @@ export function cache(opts: CacheOptions) {
(cacheConfig.get === true ||
cached._cache.cached_at + cacheConfig.get.max > Date.now());
if (useCache) {
logger.log(
`Found cached value with age ${cached._cache.cached_at.toFixed(0)}ms: `,
logger.log(`Found cached value for model ${model} with operation ${operation}`);
logger.debug(
`Found cached value with age ${(
new Date().getTime() - cached._cache.cached_at
).toFixed(0)}ms: `,
cached
);
return cached.result;
Expand All @@ -91,7 +95,8 @@ export function cache(opts: CacheOptions) {
const cached_at = Date.now();
const expires_at = cacheConfig.set === true ? false : cached_at + cacheConfig.set.ttl;
const pack = { result: base, _cache: { cached_at, expires_at } };
logger.log(`Set cache: `, pack);
logger.log(`Set cache for model ${model} with operation ${operation}`);
logger.debug(`Set cache: `, pack);
await db.set({ model, operation, args }, pack);
}

Expand Down
6 changes: 3 additions & 3 deletions src/storage/acebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export function adapterAceBase(acebase: AceBase): Adapter {
return ({ logger }) => ({
async get({ model, operation, args }) {
const queryHash = hash({ operation, args });
logger.module('acebase').log(`Get hash ${queryHash}`);
logger.module('acebase').debug(`Get hash ${queryHash}`);

const snapshot = await acebase.ref(`cache/${model}/${queryHash}`).get();
if (snapshot.exists()) {
logger.module('acebase').log(`Found snapshot:`, snapshot.val());
logger.module('acebase').debug(`Found snapshot:`, snapshot.val());
const value = snapshot.val();
// If null was provided Acebase does not save it at all
if (value.result === undefined) {
Expand All @@ -22,7 +22,7 @@ export function adapterAceBase(acebase: AceBase): Adapter {
},
async set({ model, operation, args }, value) {
const queryHash = hash({ operation, args });
logger.module('acebase').log(`Set hash ${queryHash}`);
logger.module('acebase').debug(`Set hash ${queryHash}`);
await acebase.ref(`cache/${model}/${queryHash}`).set(value);
},
async clear(model) {
Expand Down
6 changes: 3 additions & 3 deletions src/storage/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export function adapterMemory(): Adapter {
const queryHash = hash({ operation, args });
const key = `${model}:${queryHash}`;

logger.module('memory').log(`Get key ${key}`);
logger.module('memory').debug(`Get key ${key}`);
const value = storage.get(key);
if (value) {
logger.module('memory').log(`Found value:`, value);
logger.module('memory').debug(`Found value:`, value);
return JSON.parse(value);
}
return null;
Expand All @@ -20,7 +20,7 @@ export function adapterMemory(): Adapter {
const queryHash = hash({ operation, args });
const key = `${model}:${queryHash}`;

logger.module('memory').log(`Set key ${key}`);
logger.module('memory').debug(`Set key ${key}`);
storage.set(key, JSON.stringify(value));
},
async clear(model) {
Expand Down
29 changes: 25 additions & 4 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import chalk, { ChalkInstance } from 'chalk';

export type LogLevel = 'log' | 'warn' | 'error';
export type LogLevel = 'debug' | 'log' | 'warn' | 'error';

const Purple = chalk.hex('#9900EF');
const Gray = chalk.hex('#333333');
const Green = chalk.hex('#57F2A5');
const Red = chalk.hex('#F93131');
Expand All @@ -18,12 +19,27 @@ export class Logger {
return logger;
}

/**
* Log a debug message to stdout
* @param args The arguments to log
*/
public debug(...args: unknown[]): void {
if (this.logLevel === 'debug') {
this.write({
type: 'debug',
color: Purple,
args,
method: console.debug
});
}
}

/**
* Log a message to stdout
* @param message The message to log
*/
public log(...args: unknown[]): void {
if (this.logLevel === 'log') {
if (this.logLevel === 'debug' || this.logLevel === 'log') {
this.write({
type: 'log',
color: Green,
Expand All @@ -38,7 +54,7 @@ export class Logger {
* @param message The message to log
*/
public warn(...args: unknown[]): void {
if (this.logLevel === 'log' || this.logLevel === 'warn') {
if (this.logLevel === 'debug' || this.logLevel === 'log' || this.logLevel === 'warn') {
this.write({
type: 'warn',
color: Orange,
Expand All @@ -53,7 +69,12 @@ export class Logger {
* @param message The message to log
*/
public error(...args: unknown[]): void {
if (this.logLevel === 'log' || this.logLevel === 'warn' || this.logLevel === 'error') {
if (
this.logLevel === 'debug' ||
this.logLevel === 'log' ||
this.logLevel === 'warn' ||
this.logLevel === 'error'
) {
this.write({
type: 'error',
color: Red,
Expand Down

0 comments on commit 1569c09

Please sign in to comment.