Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement custom logger #2521

Merged
merged 6 commits into from
Aug 2, 2021
Merged

feat: implement custom logger #2521

merged 6 commits into from
Aug 2, 2021

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 antfu and GrygrFlzr 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-dev patak-dev changed the title feat(custom-logger): implement custom logger feat: implement custom logger Aug 2, 2021
@patak-dev patak-dev 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