@code-editor/console-feed

中文文档
3.3.1发布于 2022/04/13MITTypeScriptCommonJS近 30 天下载 2071

A React component that displays console logs from the current page, an iframe or transported across a server

原始使用文档
文档目录16 节
  1. Who's using it
  2. Features
  3. Install
  4. Basic usage
  5. Props for <Console /> component
  6. logs: Log[]
  7. filter?: Methods[]
  8. variant?: 'light' | 'dark'
  9. styles?: Styles
  10. searchKeywords?: string
  11. logFilter?: Function
  12. Log methods
  13. Log object
  14. Serialization
  15. Disabling serialization
  16. Developing

console-feed Sponsors

Sponsor this project

npm version CircleCI npm downloads Demo

A React component that displays console logs from the current page, an iframe or transported across a server.

Demo

Who's using it

Features

  • Console formatting - style and give your logs color, and makes links clickable
  • DOM nodes - easily inspect & expand HTML elements, with syntax highlighting
  • console.table - view your logs in a table format
  • Other console methods:
    • console.time - view the time in milliseconds it takes to complete events
    • console.assert - assert that a statement is truthy
    • console.count - count how many times something occurs
  • Inbuilt JSON serialization - Objects, Functions & DOM elements can be encoded / decoded to and from JSON

Install

yarn add console-feed
# or
npm install console-feed

Basic usage

CodeSandbox

import React from 'react'
import { Hook, Console, Decode } from 'console-feed'

class App extends React.Component {
  state = {
    logs: [],
  }

  componentDidMount() {
    Hook(window.console, (log) => {
      this.setState(({ logs }) => ({ logs: [...logs, Decode(log)] }))
    })

    console.log(`Hello world!`)
  }

  render() {
    return (
      <div style={{ backgroundColor: '#242424' }}>
        <Console logs={this.state.logs} variant="dark" />
      </div>
    )
  }
}

OR with hooks:

import React, { useState, useEffect } from 'react'
import { Console, Hook, Unhook } from 'console-feed'

const LogsContainer = () => {
  const [logs, setLogs] = useState([])

  // run once!
  useEffect(() => {
    Hook(
      window.console,
      (log) => setLogs((currLogs) => [...currLogs, log]),
      false
    )
    return () => Unhook(window.console)
  }, [])

  return <Console logs={logs} variant="dark" />
}

export { LogsContainer }

Props for <Console /> component

logs: Log[]

An array consisting of Log objects. Required

filter?: Methods[]

Filter the logs, only displaying messages of certain methods.

variant?: 'light' | 'dark'

Sets the font color for the component. Default - light

styles?: Styles

Defines the custom styles to use on the component - see Styles.d.ts

searchKeywords?: string

A string value to filter logs

logFilter?: Function

If you want to use a custom log filter function, you can provide your own implementation

Log methods

Each log has a method assigned to it. The method is used to determine the style of the message and for the filter prop.

type Methods =
  | 'log'
  | 'debug'
  | 'info'
  | 'warn'
  | 'error'
  | 'table'
  | 'clear'
  | 'time'
  | 'timeEnd'
  | 'count'
  | 'assert'

Log object

A log object consists of the following:

type Logs = Log[]

interface Log {
  // The log method
  method: Methods
  // The arguments passed to console API
  data: any[]
}

Serialization

By default when you use the Hook() API, logs are serialized so that they will safely work with JSON.stringify. In order to restore a log back to format compatible with the <Console /> component, you need to call the Decode() method.

Disabling serialization

If the Hook function and the <Console /> component are on the same origin, you can disable serialization to increase performance.

Hook(
  window.console,
  (log) => {
    this.setState(({ logs }) => ({ logs: [...logs, log] }))
  },
  false
)

Developing

To run console-feed locally, simply run:

yarn
yarn start
yarn test:watch

Head over to http://localhost:3000 in your browser, and you'll see the demo page come up. After you make changes you'll need to reload, but the jest tests will automatically restart.

版本与发布记录

@code-editor/console-feed 共发布 2 个版本,首次发布于 2022/04/13;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.3.1latest2022/04/134 年前
3.3.02022/04/134 年前

关于 @code-editor/console-feed 的常见问题

@code-editor/console-feed 是什么?

A React component that displays console logs from the current page, an iframe or transported across a server

如何安装 @code-editor/console-feed?

在项目目录运行 npm install @code-editor/console-feed,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@emotion/react(^11.x)、@emotion/styled(^11.x)、react(^15.x || ^16.x || ^17.x || ^18.x )。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@code-editor/console-feed 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@code-editor/console-feed 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@code-editor/console-feed 可以商用吗?

@code-editor/console-feed 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@code-editor/console-feed 的下载量有多少?

根据 npm 官方下载统计,@code-editor/console-feed 在 2026/10/10 – 2026/11/08 这 30 天内被下载 2,071 次,日均约 69 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@code-editor/console-feed 还在维护吗?

最新版本 3.3.1 发布于 2022/04/13(4 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/04/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。