Skip to content

Commit

Permalink
🚧 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
maraisr committed May 23, 2024
1 parent 7a8451d commit b20b88f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 30 deletions.
12 changes: 9 additions & 3 deletions example.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { diary, LogFn, type OnEmitFn } from './src/mod.ts';
import { diary, type LogFn, type OnEmitFn } from './src/mod.ts';
import { browser, plain, pretty } from './src/output.console.ts';
import { interpolate } from './src/util.ts';

Expand All @@ -16,7 +16,10 @@ function example(oe: OnEmitFn) {
let user = new User();

// Example where you may want to create a standard logger and output fn for your application
let createLogger = (ctx: { loggerName: string; pid: number }): LogFn<typeof ctx> => {
let createLogger = (ctx: {
loggerName: string;
pid: number;
}): LogFn<typeof ctx> => {
return diary((level, message, props = {}) => {
return oe(level, message, { ...ctx, ...props });
});
Expand All @@ -42,7 +45,10 @@ function example(oe: OnEmitFn) {
log('info', 'this {user} exists', { user });
log('info', 'we call that user {name} with {id}', user);

log('info', 'we also have inherited props like {pid}', { ...user, pid: Deno.pid });
log('info', 'we also have inherited props like {pid}', {
...user,
pid: Deno.pid,
});

log('info', 'this {user} can send more properties than we want', {
user,
Expand Down
7 changes: 3 additions & 4 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ await build({

filterDiagnostic(diag) {
let txt = diag.messageText.toString();
return !txt.includes(
// ignore type error for missing Deno built-in information
`Type 'ReadableStream<Event>' must have a '[Symbol.asyncIterator]()' method that returns an async iterator`,
) && !txt.includes(
// ignore type error for missing Deno built-in information

return !/Type 'ReadableStream<.*>' must have a/.test(txt) && !txt.includes(
`Type 'Timeout' is not assignable to type 'number'.`,
);
},
Expand Down
2 changes: 1 addition & 1 deletion src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ export interface LogFn<Ctx = unknown> {
}

export function diary(onEmit: OnEmitFn): LogFn<unknown> {
return onEmit.bind(onEmit);
return onEmit;
}
34 changes: 14 additions & 20 deletions src/output.console.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { magenta } from 'jsr:@std/fmt@^0.224.0/colors';
import type { Level } from './mod.ts';
import { interpolate } from './util.ts';

Expand All @@ -22,33 +23,26 @@ function log(out: string, level: Level, message: string, props = {}) {
console[level === 'fatal' ? 'error' : level](out, ...args);
}

export function browser(
level: Level,
message: string,
props?: object | undefined,
) {
export function browser(level: Level, message: string, props?: object | undefined) {
log('', level, message, props);
}

export function plain(
level: Level,
message: string,
props?: object | undefined,
) {
export function plain(level: Level, message: string, props?: object | undefined) {
log(LEVELS[level], level, message, props);
}

export function pretty(
level: Level,
message: string,
props?: object | undefined,
) {
export function pretty(level: Level, message: string, props?: object | undefined) {
let l = LEVELS[level] as string;
if (level === 'debug') l = gray(l);
if (level === 'info') l = blue(l);
if (level === 'warn') l = yellow(l);
if (level === 'error') l = red(l);
if (level === 'fatal') l = bold(red(l));

// deno-fmt-ignore
switch (level) {
case 'log': l = gray(l); break;
case 'debug': l = magenta(l); break;
case 'warn': l = yellow(l); break;
case 'info': l = blue(l); break;
case 'error': l = red(l); break;
case 'fatal': l = bold(red(l)); break;
}

log(l, level, message, props);
}
4 changes: 2 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export type Pretty<T> = { [K in keyof T]: T[K] } & {};
export type Pretty<T> = { [K in keyof T]: T[K] } & unknown;

export type Props<T> = T extends `${string}{${infer S}}${infer Rest}`
? { [K in S]: unknown } & Props<Rest>
: {};
: unknown;

// @internal
export type Dict<T = unknown> = Record<string, T>;
Expand Down

0 comments on commit b20b88f

Please sign in to comment.