Skip to content

Commit 1569c09

Browse files
committed
fix: better log methods
1 parent 210a87d commit 1569c09

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export function cache(opts: CacheOptions) {
6868

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

7273
if (cacheConfig.get) {
7374
const cached = await db.get<CachedResult>({ model, operation, args });
@@ -77,8 +78,11 @@ export function cache(opts: CacheOptions) {
7778
(cacheConfig.get === true ||
7879
cached._cache.cached_at + cacheConfig.get.max > Date.now());
7980
if (useCache) {
80-
logger.log(
81-
`Found cached value with age ${cached._cache.cached_at.toFixed(0)}ms: `,
81+
logger.log(`Found cached value for model ${model} with operation ${operation}`);
82+
logger.debug(
83+
`Found cached value with age ${(
84+
new Date().getTime() - cached._cache.cached_at
85+
).toFixed(0)}ms: `,
8286
cached
8387
);
8488
return cached.result;
@@ -91,7 +95,8 @@ export function cache(opts: CacheOptions) {
9195
const cached_at = Date.now();
9296
const expires_at = cacheConfig.set === true ? false : cached_at + cacheConfig.set.ttl;
9397
const pack = { result: base, _cache: { cached_at, expires_at } };
94-
logger.log(`Set cache: `, pack);
98+
logger.log(`Set cache for model ${model} with operation ${operation}`);
99+
logger.debug(`Set cache: `, pack);
95100
await db.set({ model, operation, args }, pack);
96101
}
97102

src/storage/acebase.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export function adapterAceBase(acebase: AceBase): Adapter {
66
return ({ logger }) => ({
77
async get({ model, operation, args }) {
88
const queryHash = hash({ operation, args });
9-
logger.module('acebase').log(`Get hash ${queryHash}`);
9+
logger.module('acebase').debug(`Get hash ${queryHash}`);
1010

1111
const snapshot = await acebase.ref(`cache/${model}/${queryHash}`).get();
1212
if (snapshot.exists()) {
13-
logger.module('acebase').log(`Found snapshot:`, snapshot.val());
13+
logger.module('acebase').debug(`Found snapshot:`, snapshot.val());
1414
const value = snapshot.val();
1515
// If null was provided Acebase does not save it at all
1616
if (value.result === undefined) {
@@ -22,7 +22,7 @@ export function adapterAceBase(acebase: AceBase): Adapter {
2222
},
2323
async set({ model, operation, args }, value) {
2424
const queryHash = hash({ operation, args });
25-
logger.module('acebase').log(`Set hash ${queryHash}`);
25+
logger.module('acebase').debug(`Set hash ${queryHash}`);
2626
await acebase.ref(`cache/${model}/${queryHash}`).set(value);
2727
},
2828
async clear(model) {

src/storage/memory.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ export function adapterMemory(): Adapter {
88
const queryHash = hash({ operation, args });
99
const key = `${model}:${queryHash}`;
1010

11-
logger.module('memory').log(`Get key ${key}`);
11+
logger.module('memory').debug(`Get key ${key}`);
1212
const value = storage.get(key);
1313
if (value) {
14-
logger.module('memory').log(`Found value:`, value);
14+
logger.module('memory').debug(`Found value:`, value);
1515
return JSON.parse(value);
1616
}
1717
return null;
@@ -20,7 +20,7 @@ export function adapterMemory(): Adapter {
2020
const queryHash = hash({ operation, args });
2121
const key = `${model}:${queryHash}`;
2222

23-
logger.module('memory').log(`Set key ${key}`);
23+
logger.module('memory').debug(`Set key ${key}`);
2424
storage.set(key, JSON.stringify(value));
2525
},
2626
async clear(model) {

src/utils/logger.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import chalk, { ChalkInstance } from 'chalk';
22

3-
export type LogLevel = 'log' | 'warn' | 'error';
3+
export type LogLevel = 'debug' | 'log' | 'warn' | 'error';
44

5+
const Purple = chalk.hex('#9900EF');
56
const Gray = chalk.hex('#333333');
67
const Green = chalk.hex('#57F2A5');
78
const Red = chalk.hex('#F93131');
@@ -18,12 +19,27 @@ export class Logger {
1819
return logger;
1920
}
2021

22+
/**
23+
* Log a debug message to stdout
24+
* @param args The arguments to log
25+
*/
26+
public debug(...args: unknown[]): void {
27+
if (this.logLevel === 'debug') {
28+
this.write({
29+
type: 'debug',
30+
color: Purple,
31+
args,
32+
method: console.debug
33+
});
34+
}
35+
}
36+
2137
/**
2238
* Log a message to stdout
2339
* @param message The message to log
2440
*/
2541
public log(...args: unknown[]): void {
26-
if (this.logLevel === 'log') {
42+
if (this.logLevel === 'debug' || this.logLevel === 'log') {
2743
this.write({
2844
type: 'log',
2945
color: Green,
@@ -38,7 +54,7 @@ export class Logger {
3854
* @param message The message to log
3955
*/
4056
public warn(...args: unknown[]): void {
41-
if (this.logLevel === 'log' || this.logLevel === 'warn') {
57+
if (this.logLevel === 'debug' || this.logLevel === 'log' || this.logLevel === 'warn') {
4258
this.write({
4359
type: 'warn',
4460
color: Orange,
@@ -53,7 +69,12 @@ export class Logger {
5369
* @param message The message to log
5470
*/
5571
public error(...args: unknown[]): void {
56-
if (this.logLevel === 'log' || this.logLevel === 'warn' || this.logLevel === 'error') {
72+
if (
73+
this.logLevel === 'debug' ||
74+
this.logLevel === 'log' ||
75+
this.logLevel === 'warn' ||
76+
this.logLevel === 'error'
77+
) {
5778
this.write({
5879
type: 'error',
5980
color: Red,

0 commit comments

Comments
 (0)