Skip to content

Commit

Permalink
createLogger with default writer
Browse files Browse the repository at this point in the history
  • Loading branch information
wenerme committed Dec 16, 2022
1 parent 42ab8a9 commit 91a284d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
8 changes: 4 additions & 4 deletions apps/demo/src/components/cn/ParserCard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Button } from '../../daisy';
import { Parser } from './parseIt';
import type { Parser } from './parseIt';

export const ParserCard: React.FC<{
parser: Parser;
Expand All @@ -12,14 +12,14 @@ export const ParserCard: React.FC<{
if (parsed) {
if ('prev' in parsed) {
actions.push(
<Button size={'sm'} onClick={() => onChange?.(parsed.prev().toString())}>
<Button key={'prev'} size={'sm'} onClick={() => onChange?.(parsed.prev().toString())}>
上一位
</Button>,
);
}
if ('next' in parsed) {
actions.push(
<Button size={'sm'} onClick={() => onChange?.(parsed.next().toString())}>
<Button key={'next'} size={'sm'} onClick={() => onChange?.(parsed.next().toString())}>
下一位
</Button>,
);
Expand All @@ -28,7 +28,7 @@ export const ParserCard: React.FC<{
if (model) {
if ('random' in model) {
actions.push(
<Button size={'sm'} onClick={() => onChange?.(model.random().toString())}>
<Button key={'random'} size={'sm'} onClick={() => onChange?.(model.random().toString())}>
随机
</Button>,
);
Expand Down
5 changes: 1 addition & 4 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wener/utils",
"version": "1.1.8",
"version": "1.1.9",
"type": "module",
"description": "Utils for daily use",
"repository": {
Expand All @@ -10,9 +10,6 @@
"homepage": "https://github.com/wenerme/wode#readme",
"author": "wener",
"license": "MIT",
"engines": {
"node": "16+"
},
"main": "dist/cjs/index.cjs",
"module": "lib/index.js",
"exports": {
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export { isModule, type Module } from './modules/isModule';

// logging
export { type Logger, type LogLevel } from './logging/Logger';
export { createWriteLogger } from './logging/createWriteLogger';
export { createLogger } from './logging/createLogger';
export { createNoopLogger } from './logging/createNoopLogger';
export { createChildLogger } from './logging/createChildLogger';

Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/logging/createChildLogger.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Logger, LoggerWithChild } from './Logger';
import { createWriteLogger } from './createWriteLogger';
import { createLogger } from './createLogger';

export function createChildLogger(l: Logger, ctx: object): LoggerWithChild {
if (l.child) {
return l.child(ctx) as LoggerWithChild;
}
return createWriteLogger((o) => {
return createLogger((o) => {
const { level, values, ...c } = o;
if (Object.keys(c).length) {
l[level](c, ...values);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import type { LoggerWithChild, LogLevel } from './Logger';

export function createWriteLogger(
write: (o: { level: LogLevel; values: any[] } & Record<string | symbol, any>) => void,
export function createLogger(
write: (o: { level: LogLevel; values: any[] } & Record<string | symbol, any>) => void = ({
level,
values,
...ctx
}) => {
({ values, ...ctx } = merge(ctx, values));
console[level]?.(...values, ctx);
},
context: object = {},
): LoggerWithChild {
return {
Expand All @@ -10,6 +17,15 @@ export function createWriteLogger(
info: (...values) => write({ ...context, level: 'info', values }),
warn: (...values) => write({ ...context, level: 'warn', values }),
error: (...values) => write({ ...context, level: 'error', values }),
child: (ctx) => createWriteLogger(write, { ...context, ...ctx }),
child: (ctx) => createLogger(write, { ...context, ...ctx }),
};
}

// logger.info({name:'wener'},'message')
// merge initial context with message object
function merge(ctx: any, values: any[]) {
if (values[0] && typeof values[0] === 'object') {
return { ...ctx, ...values[0], values: values.slice(1) };
}
return { ...ctx, values };
}
8 changes: 5 additions & 3 deletions packages/utils/src/logging/logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import test from 'ava';
import { createChildLogger } from './createChildLogger';
import { createWriteLogger } from './createWriteLogger';
import { createLogger } from './createLogger';

test('logger', (t) => {
{
const logs: any[] = [];
const base = createWriteLogger((o) => logs.push(o));
const base = createLogger((o) => logs.push(o));
const l = createChildLogger(base, { c: 'test' });
l.info('hello');
t.deepEqual(logs.shift(), { level: 'info', values: ['hello'], c: 'test' });
Expand All @@ -15,7 +15,7 @@ test('logger', (t) => {
createChildLogger(console, { c: 'test' }).info('hello');
{
let pass = 0;
const l = createWriteLogger(
const l = createLogger(
(o) => {
pass++;
t.log(`${o.level}: [${[o.m, o.c].filter(Boolean).join('.') || 'default'}]`, ...o.values);
Expand All @@ -30,5 +30,7 @@ test('logger', (t) => {
t.is(pass, 2);
createChildLogger(l, { m: 'Child' }).info('nice 3');
t.is(pass, 3);

createLogger().child({ name: 'wener' }).info({ x: 1 }, 'Nice');
}
});

1 comment on commit 91a284d

@vercel
Copy link

@vercel vercel bot commented on 91a284d Dec 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

wode – ./

wode.vercel.app
wode-wener.vercel.app
wode-git-main-wener.vercel.app

Please sign in to comment.