How to set up a console sink in a TypeScript application?

Oct 27, 2025

Leave a message

Hey there! If you're into TypeScript applications and looking to set up a console sink, you've come to the right place. I'm a supplier of console sinks, and I'm here to walk you through the process step by step.

First off, let's understand what a console sink is. In the context of a TypeScript application, a console sink is a way to handle and manage the output of your application's logs. It's like a little bucket where all your log messages go, and you can decide what to do with them - whether it's displaying them on the console, sending them to a file, or even forwarding them to a remote server.

Prerequisites

Before we dive into setting up a console sink, you need to have a basic TypeScript project up and running. If you haven't already, you can create one using tools like npm and tsc (TypeScript compiler). Here's a quick rundown of how to get started:

  1. Initialize a new npm project:
    Open your terminal and run the following command:

    npm init -y
    

    This will create a package.json file in your project directory, which keeps track of all your project's dependencies.

  2. Install TypeScript:
    Run the following command to install TypeScript as a development dependency:

    npm install --save-dev typescript
    
  3. Create a tsconfig.json file:
    You can generate a basic tsconfig.json file by running:

    npx tsc --init
    

    This file contains the configuration options for the TypeScript compiler.

Setting up a Simple Console Sink

Now that you have your TypeScript project ready, let's start setting up a simple console sink. We'll use a basic example where we log messages to the console.

  1. Create a Logger Class
    First, let's create a Logger class that will handle the logging. Create a new file, for example, logger.ts, and add the following code:

    Commercial Ceramic Vessel Sink Bathroom Set With Industrial Stainless Steel Frame &Wall-Mount Bracket

    class Logger {
        log(message: string) {
            console.log(message);
        }
    
        error(message: string) {
            console.error(message);
        }
    }
    
    export default Logger;
    

    In this class, we have two methods: log and error. The log method simply logs a message to the console using console.log, and the error method logs an error message using console.error.

  2. Using the Logger in Your Application
    Now, let's use our Logger class in our main application file. Create a new file, say app.ts, and add the following code:

    import Logger from './logger';
    
    const logger = new Logger();
    
    logger.log('This is a log message');
    logger.error('This is an error message');
    

    Here, we import the Logger class, create an instance of it, and then use the log and error methods to log messages.

Advanced Console Sink Setup

While the above example is a simple way to log messages to the console, you might want to do more advanced things, like filtering messages based on their severity or sending them to multiple destinations.

  1. Adding Log Levels
    Let's add log levels to our Logger class. We'll define different levels like DEBUG, INFO, WARN, and ERROR. Update the logger.ts file as follows:

    enum LogLevel {
        DEBUG,
        INFO,
        WARN,
        ERROR
    }
    
    class Logger {
        private logLevel: LogLevel;
    
        constructor(logLevel: LogLevel = LogLevel.INFO) {
            this.logLevel = logLevel;
        }
    
        log(level: LogLevel, message: string) {
            if (level >= this.logLevel) {
                switch (level) {
                    case LogLevel.DEBUG:
                        console.debug(message);
                        break;
                    case LogLevel.INFO:
                        console.log(message);
                        break;
                    case LogLevel.WARN:
                        console.warn(message);
                        break;
                    case LogLevel.ERROR:
                        console.error(message);
                        break;
                }
            }
        }
    
        debug(message: string) {
            this.log(LogLevel.DEBUG, message);
        }
    
        info(message: string) {
            this.log(LogLevel.INFO, message);
        }
    
        warn(message: string) {
            this.log(LogLevel.WARN, message);
        }
    
        error(message: string) {
            this.log(LogLevel.ERROR, message);
        }
    }
    
    export default Logger;
    

    In this updated code, we've added an enum for log levels and a constructor parameter to set the minimum log level. The log method now checks if the log level of the message is greater than or equal to the minimum log level before logging it.

  2. Using the Advanced Logger
    Update the app.ts file to use the advanced Logger class:

    import Logger, { LogLevel } from './logger';
    
    const logger = new Logger(LogLevel.DEBUG);
    
    logger.debug('This is a debug message');
    logger.info('This is an info message');
    logger.warn('This is a warning message');
    logger.error('This is an error message');
    

    Now, you can control which messages get logged based on the log level you set when creating the Logger instance.

Sending Logs to Multiple Destinations

Sometimes, you might want to send your log messages to multiple destinations, like the console and a file. To do this, we can use a concept called "sinks". A sink is a destination where log messages are sent.

  1. Creating a Sink Interface
    First, let's create an interface for sinks. Create a new file, sink.ts, and add the following code:

    interface Sink {
        write(message: string): void;
    }
    
    export default Sink;
    

    This interface defines a single method, write, which is used to send a log message to the sink.

  2. Creating a Console Sink
    Now, let's create a console sink that implements the Sink interface. Update the logger.ts file as follows:

    import Sink from './sink';
    
    enum LogLevel {
        DEBUG,
        INFO,
        WARN,
        ERROR
    }
    
    class ConsoleSink implements Sink {
        write(message: string) {
            console.log(message);
        }
    }
    
    class Logger {
        private logLevel: LogLevel;
        private sinks: Sink[];
    
        constructor(logLevel: LogLevel = LogLevel.INFO, sinks: Sink[] = [new ConsoleSink()]) {
            this.logLevel = logLevel;
            this.sinks = sinks;
        }
    
        log(level: LogLevel, message: string) {
            if (level >= this.logLevel) {
                const formattedMessage = `[${LogLevel[level]}] ${message}`;
                this.sinks.forEach(sink => sink.write(formattedMessage));
            }
        }
    
        debug(message: string) {
            this.log(LogLevel.DEBUG, message);
        }
    
        info(message: string) {
            this.log(LogLevel.INFO, message);
        }
    
        warn(message: string) {
            this.log(LogLevel.WARN, message);
        }
    
        error(message: string) {
            this.log(LogLevel.ERROR, message);
        }
    }
    
    export { LogLevel };
    export default Logger;
    

    In this updated code, we've created a ConsoleSink class that implements the Sink interface. The Logger class now takes an array of sinks in its constructor, and when logging a message, it sends the message to all the sinks in the array.

  3. Adding a File Sink
    Let's add a file sink to our application. We'll use the fs module in Node.js to write log messages to a file. Update the sink.ts file as follows:

    import * as fs from 'fs';
    
    interface Sink {
        write(message: string): void;
    }
    
    class ConsoleSink implements Sink {
        write(message: string) {
            console.log(message);
        }
    }
    
    class FileSink implements Sink {
        private filePath: string;
    
        constructor(filePath: string) {
            this.filePath = filePath;
        }
    
        write(message: string) {
            fs.appendFile(this.filePath, message + '\n', (err) => {
                if (err) {
                    console.error('Error writing to file:', err);
                }
            });
        }
    }
    
    export { ConsoleSink, FileSink };
    export default Sink;
    

    Now, update the app.ts file to use both the console sink and the file sink:

    import Logger, { LogLevel } from './logger';
    import { ConsoleSink, FileSink } from './sink';
    
    const consoleSink = new ConsoleSink();
    const fileSink = new FileSink('app.log');
    
    const logger = new Logger(LogLevel.DEBUG, [consoleSink, fileSink]);
    
    logger.debug('This is a debug message');
    logger.info('This is an info message');
    logger.warn('This is a warning message');
    logger.error('This is an error message');
    

    With this setup, log messages will be sent to both the console and the app.log file.

Commercial Ceramic Vessel Sink Bathroom Set

If you're also interested in Commercial Ceramic Vessel Sink Bathroom Set, we've got you covered. Our high - quality console sinks are not only stylish but also durable, perfect for any commercial or residential bathroom.

Conclusion

Setting up a console sink in a TypeScript application can be as simple or as complex as you need it to be. Whether you're just logging messages to the console or sending them to multiple destinations, the concepts we've covered here can help you build a robust logging system.

If you're interested in our console sink products, whether it's for your TypeScript application's logging needs or for your bathroom, feel free to reach out for a procurement discussion. We're here to help you find the best solution for your requirements.

References

  • TypeScript Documentation
  • Node.js Documentation
Grace Taylor
Grace Taylor
Grace is a procurement manager at Guangdong Yingjie. She is responsible for sourcing high - quality raw materials for the production of sanitary ware, ensuring the quality of the final products from the source.
Send Inquiry