Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
typescript singleton with dependecy

I'm building an app, where i'm creating a logger as a singleton, the problem that i'm facing is that i need to initialise this singleton with data from ENV and i can only do it in a specific place of my code.

Maybe it's not really a singleton since it's returning a different type.

Is it okay to use it like this ? if not do you have any recommandations ?

Thank you.

import CustomLogger, from "./logger"
import ICustomLogger from "./Ilogger"
type LoggerConfig={level:string,format:'json'|'raw'}
class Logger{
    private static logger:ICustomLogger;
    private static config: LoggerConfig

    public static init(config:LoggerConfig){
        this.config = config
    }
    public getInstance():ICustomLogger{
        if(!Logger.config){
            throw new Error('Config not defined')
        }
        if(!Logger.logger){
            Logger.logger = new CustomLogger(config)
        }
        return Logger.logger
    }

}
about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!