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

How to use with typescript? #144

Open
sarahzinger opened this issue Oct 1, 2020 · 2 comments
Open

How to use with typescript? #144

sarahzinger opened this issue Oct 1, 2020 · 2 comments

Comments

@sarahzinger
Copy link

Hello there,

Does winston-syslog support typescript? I see there are is a @types/winston-syslog but I can't seem to get it working.

In my package.json I have

  • "winston": "^3.3.3",
  • "winston-syslog": "^2.4.4",
  • "@types/winston-syslog": "^2.0.3",
  • "typescript": "^3.9.7",

and am trying to run:

import * as winston from 'winston'

import { Syslog } from 'winston-syslog';

winston.add(new winston.transports.Syslog());

and get back Property 'Syslog' does not exist on type 'Transports'.

I can run

const winston = require('winston');

require('winston-syslog').Syslog;

winston.add(new winston.transports.Syslog());

as described in the documentation, but then I lose all type definitions and all of winston/winston-syslog is typed as "any"

I see an issue was previously opened in the winston repo, and the suggestion was to direct the question here so I figured I'd ask here.

Thanks so much for your help!

@sebsastianek
Copy link

Hi, my quick solution with typings was to merge the interfaces as follows

import * as winston from "winston";
import {Transports} from "winston/lib/winston/transports";
import {SyslogTransportInstance} from "winston-syslog";

require('winston-syslog').Syslog;

/** hacking types for injected syslog transport :( */
const winstonTransports = winston.transports as Transports & {Syslog: SyslogTransportInstance};

then just refer to typed winstonTrasports ie. add(new winstonTransports.Syslog())

@nyotiemon
Copy link

I did it this way:

import { createLogger } from 'winston';
import { SyslogTransportOptions, Syslog } from 'winston-syslog';

const opt: SyslogTransportOptions = {
    host: 'localhost',
    port: 1234,
    protocol: 'tcp4',
    facility: 'local0',
    app_name: 'My App',
    eol: '\n'
};

// and then either this way:
const logger = createLogger({ levels: winston.config.syslog.levels });
const transport = new Syslog(opt);
logger.add(transport);

// or this way:
const logger = createLogger({ levels: winston.config.syslog.levels, tranports: [new Syslog(opt)] });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants