question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to extend Logger class.

See original GitHub issue

What is the problem?

I am trying to extend winston logger with custom methods on typescript. Currently it can only be done via as any and assigning properties to the object manually. Moreover, methods like Logger#child require to be improved.

Here is how it looks like:

interface LoggerExtras {
  benchmark: Benchmark;
  child(options: Record<string, any>): this;
}
type Logger = winston.Logger & LoggerExtras;
const logger: Logger = createLogger() as any;

logger.benchmark = ...

export default logger;

What’s the feature?

Ideally, I want to be able to extend Logger in a pretty OOP way without manual property assignment manipulations like:

class MyLogger extends winston.Logger {
  benchmark(...) {
    ....
  }
}
export default new MyLogger(options);

Is it possible to add such a functionality and ensure typescript is supported?

What problem is the feature intended to solve?

Make winston logger more extensible in a clear way that is common for OOP language.

Is the absence of this feature blocking you or your team? If so, how?

It is not a blockers as there are hacks to achieve that.

Is this feature similar to an existing feature in another tool?

Yes, this feature is common feature in any OOP friendly logger where you can just use inheritance as a pattern. It is very unusual that making Logger is done through a factory method but not a constructor. Most loggers just expose a Logger class to you like so: https://ruby-doc.org/stdlib-2.4.0/libdoc/logger/rdoc/Logger.html

Is this a feature you’re prepared to implement, with support from us?

Yes.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:6
  • Comments:5

github_iconTop GitHub Comments

3reactions
Datzu712commented, Oct 17, 2021

I found a not elegant solution …

import winston from "winston"

// @ts-ignore
class Logger extends (createLogger as winston.Logger) {
    constructor(config: winston.LoggerOptions) {
        super(config)
        
    }
}

console.log(new Logger({}))

That works for me, but I don’t like use @ts-ignore and extends by a function. Note: If you remove @ts-ignore, you got an error…

0reactions
Faithfindercommented, Sep 10, 2022

I’m just reaching for it deep into the package

import { transports, format, Logger } from 'winston';
//@ts-expect-error Winston doesn't export the Logger class but it's easy to reach for it. See https://github.com/winstonjs/winston/issues/2170
import HiddenLogger from 'winston/lib/winston/logger';
// Easy to reach for, but hard to type.
const LoggerClass = HiddenLogger as typeof Logger;
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to extend the logging.Logger Class? - python
I am trying to the find the sweet point between having a Logger Class , yet still be able to use the built...
Read more >
logging — Logging facility for Python — Python 3.11.1 ...
This module defines functions and classes which implement a flexible event logging system for applications and libraries.
Read more >
Logger in Java | Java Logging Basics with Log4j and util
This article on Logger in Java is a comprehensive guide on the Java logging API used for logging solutions while creating projects.
Read more >
Java Logging Basics - The Ultimate Guide To Logging - Loggly
The PatternLayout class from Log4j and Logback supports conversion patterns, which determine how data is extracted from log events and formatted for output....
Read more >
Custom Logger class - python - Code Review Stack Exchange
It's abnormal to see Logger.filename rather than self.filename or cls.filename . If you need to guarantee it's the class' value rather than the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found