Skip to content

feat: implement custom logger#2521

Merged
patak-cat merged 6 commits intovitejs:mainfrom
stingalleman:feat/custom-logger
Aug 2, 2021
Merged

feat: implement custom logger#2521
patak-cat merged 6 commits intovitejs:mainfrom
stingalleman:feat/custom-logger

Conversation

@stingalleman
Copy link
Contributor

This PR implements a way to use your own logger.

You just need to make a class that implements the already exported class Logger and inject it in the config.

A really quick and ugly example:

import { defineConfig, Logger, LogOptions, LogType } from 'vite'
import vue from '@vitejs/plugin-vue'
import chalk from 'chalk';

// From Vite:
//
// export declare interface Logger {
//   info(msg: string, options?: LogOptions): void;
//   warn(msg: string, options?: LogOptions): void;
//   error(msg: string, options?: LogOptions): void;
//   clearScreen(type: LogType): void;
//   hasWarned: boolean;
// }

class MyLogger implements Logger {
  clearScreen(type: LogType): void {
    console.clear()
  }
  
  hasWarned: false;

  info(msg: string, options?: LogOptions): void {
    if (options?.timestamp) {
      console.log(`${chalk.red.bold('info')}: ${msg}`)
    } else {
      console.log(msg)
    }
  }
  error() { }
  warn() { }
}

export default defineConfig({
  plugins: [vue()],
  logger: new MyLogger()
})

will result in:

image

@stingalleman stingalleman marked this pull request as draft March 15, 2021 18:03
@stingalleman stingalleman marked this pull request as ready for review March 15, 2021 18:07
@Shinigami92 Shinigami92 added enhancement New feature or request p2-nice-to-have Not breaking anything but nice to have (priority) labels Mar 21, 2021
Shinigami92
Shinigami92 previously approved these changes May 28, 2021
@Shinigami92 Shinigami92 requested review from GrygrFlzr and antfu May 28, 2021 16:54
@antfu
Copy link
Member

antfu commented Jun 11, 2021

@stingalleman Can you explain a bit more about why you want this feature? We would like to keep our config interface as minimal as possible, and from our team aspect, we didn't yet see many use cases of providing custom logger here. Thanks.

@stingalleman
Copy link
Contributor Author

@antfu We run our developer environment in Docker, and the problem is that all the logs are shown at once, and it's just a nice visual addition if Vite uses the same format as the other loggers, which are all the same.

@patak-cat patak-cat changed the title feat(custom-logger): implement custom logger feat: implement custom logger Aug 2, 2021
@patak-cat patak-cat merged commit 59841f0 into vitejs:main Aug 2, 2021
@stingalleman stingalleman deleted the feat/custom-logger branch August 8, 2021 02:09
aleclarson pushed a commit to aleclarson/vite that referenced this pull request Sep 30, 2021
aleclarson pushed a commit to aleclarson/vite that referenced this pull request Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request p2-nice-to-have Not breaking anything but nice to have (priority)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants