npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

error-catch-notice

v0.1.11

Published

error log save

Downloads

4

Readme

log collector

에러 로깅, 사용자 정보 저장.

init database table

테이블 셋팅이 먼저 필요하다. 두개의 테이블 error_reports와 user_trackings이 필요 하면 아래의 방법으로 만들 수 있다. 테이블 명은 변경이 가능하나, 컬럼은 변경이 불가능하다. 기본 테이블 명은 error_reports, user_trackings

스크립트

import { initTable }  from 'log-collector'

const testConfig = {
    dbProperties:{
        host: 'xxxx',
        dialect: 'mysql',
        username: 'xxxx',
        password: 'xxxx',
    },
    slackProperties: {
        url: 'xxxx',
    },
    slackOn: true,
    dbOn: true,
};

try{ 
    await initTable(testConfig);
    await initUserTrackingTable(testConfig, 'some table name'); // 테이블 네임
} catch (error) {
    console.error(error);
}

spec: /src/utils/test/initTable.spec.ts

sql

-- auto-generated definition
create table error_reports
(
  id              bigint auto_increment
    primary key,
  error_type      varchar(25) default 'server_error' null,
  name            varchar(25)                        null,
  message         varchar(50)                        null,
  code            varchar(50)                        null,
  errno           varchar(50)                        null,
  syscall         varchar(50)                        null,
  hostname        varchar(50)                        null,
  status          int(3) unsigned                    null,
  status_text     varchar(25)                        null,
  req_url         varchar(300)                       null,
  req_query       varchar(300)                       null,
  req_body        varchar(300)                       null,
  req_method      varchar(6)                         null,
  req_status      int(3) unsigned                    null,
  req_res_data    varchar(300)                       null,
  user_id         varchar(25)                        null,
  user_name       varchar(100)                       null,
  user_department varchar(100)                       null,
  createTimestamp datetime                           not null
)
  collate = utf8_unicode_ci;

-- auto-generated definition
create table user_trakings
(
  id              bigint auto_increment
    primary key,
  display_name    varchar(100)    null,
  action_name     varchar(50)     null,
  operation       int(1) unsigned null,
  params          json            null,
  createTimestamp datetime        not null
)
  collate = utf8_unicode_ci;

how to use

error report

초기화 과정 필요. 이후 에러 리포팅.

es7 초기화

import ErrorReportor from 'log-collector'

const config = {
    slackOn: true,
    dbOn: true,
    dbProperties:{ // db info
        host: 'db host',
        dialect: 'mysql', // only mysql support
        username: 'xxx',
        password: 'xxx',
        
    },
    slackProperties: { // slack info
        url: 'webhook url',
    },
    
};
const reportor = await ErrorReporter.getInstance(config); // singleton
const error = {
    type: 'client-error',
    name: 'some error',
    ...
}; // see error format
reportor.procesError(error);

es5 - express



/**
* initialize - props
*/
let errorReporter;
function initErrorNotice() {
  try {
    const ErrorReporter = errorCatchNotice.default
    const config = {
      dbOn: true,
      slackOn: true,
      dbProperties:{
        host: 'xxxxxx',
        dialect: 'mysql',
        username: 'xxx',
        password: 'xxx',
      },
      slackProperties: {
        url: conf.slack.url,
      }
    };
    ErrorReporter.getInstance(config).then(instance => {
      errorReporter = instance
    })
  } catch (error) {
    console.error(`error initErrorNotice!!! ${error}`);
  }
}

initErrorNotice();

/**
* initialize - sequalize
*/
let errorReporter;
function initErrorNotice() {
  try {
    const ErrorReporter = errorCatchNotice.default
    const config = {
      dbOn: true,
      slackOn: true,
      slackProperties: {
        url: conf.slack.url,
      },
      sequelize: sequelize, // sequelize 인스턴스
    };
    ErrorReporter.getInstance(config).then(instance => {
      errorReporter = instance
    })
  } catch (error) {
    console.error(`error initErrorNotice!!! ${error}`);
  }
}

주요 config instance

export interface SlackProperties {
    url: string;
}

export interface Config {
    dbOn: boolean;
    slackOn: boolean;
    dbProperties?: object;
    sequelize?: any;
    slackProperties?: SlackProperties;
    tableName?: string;
}

에러 리포트

/**
* error handle
*/
app.use(function(err, req, res, next) {
  try {
    //...
    const errorInfo = {
      type: 'server-error',
      name, message, code, status, errno, syscall, hostname,
      reqInfo: {
        url, query, body, method, status, responseData: data
      },
      user: {
        id: adminId,
        name: adminName,
        department
      }
    }
    errorReporter.procesError(errorInfo).then(()=>{
    }).catch(error4Send => {
      console.log(error4Send)
    });
    res.status(status).json(errorInfo)
  } catch(e) {
    res.status(500).json(e)
  }
});
process.on('exit', function() {
  try {
    errorReporter.close() // 인스턴스를 없애고 , 시퀄라이즈 클로즈
  } catch(err) {
    console.log('errorReporter close fail',err);
  }

});

주요 ErrorInfo instance

export interface User {
    id?: string;
    name?: string
    department?: string;
}

export interface ErrorInfo {
    type: string;
    name?: string;
    message?: string;
    code?: string;
    errno?: string;
    syscall?: string;
    hostname?: string;
    status: number;
    statusText?: string;
    reqInfo?: RequestInfo;
    user?: User;
}

export interface RequestInfo {
    url: string;
    query?: object;
    body?: object;
    method: string;
    status: number;
    responseData?: string;
}

export interface User {
    id?: string;
    name?: string
    department?: string;
}

cf: src/test/ErrorReporter.spec.ts cf: src/test/ErrorReporter.dbOff.spec.ts

메서드

async procesError( error ); // 에러 처리
getSequalize(); // 시퀄라이즈 얻기
isInitialize() // 초기화 확인 - 크게 의미는 없음 . 테스트용으로 제작

user tracking

초기화

test.skip('initialize db - by sequalize', async () => {
    const sequelize = new Sequelize(dbProperties);
    const userTracker = await UserTracker.getInstance({
        sequelize,
    });
    expect(userTracker.isInitialize()).toBe(true);

    userTracker.close();
});

유저 트랙킹

test('save user tracking', async () => {
    const userTracker = await UserTracker.getInstance(configByDbproperties);
    console.log(dbTester);
    const before = await dbTester.getCount4UserTraking();
    await userTracker.save({
        displayName: 'test1',
        actionName: 'action1',
        operation: Operation.Insert,
        params: { param1: 'p1', param2: 'p2' },
    });
    await userTracker.save({
        displayName: 'test1',
        actionName: 'action1',
        operation: Operation.Delete,
        params: { param1: 'bb', param2: 'ab' },
    });
    await userTracker.save({
        displayName: 'test1',
        actionName: 'action1',
        operation: Operation.Update,
        params: { param1: 'temp', param2: 'ok' },
    });
    const after = await dbTester.getCount4UserTraking();
    expect((before + 3)).toBe(after);

});

주요 interface

/**
 * 사용자 오퍼레이션 - javascript 의 경우 순서대로 insert=>1,update=>2,delete=>3,select=>4,merge=>5
 */
export enum Operation {
    Insert = 1,
    Update,
    Delete,
    Select,
    Merge,
}

/**
 * 액션
 */
export interface ActionInfo {
    displayName?: string, //화면명
    actionName: string, // 액션 명
    operation: Operation, // 수행 오퍼레이션
    params?: object, // 파라미터
}

export interface UserTrackerConfig {
    dbProperties?: object;
    sequelize?: any; 
    saveReconnect?: boolean;
    tableName?: string;
}

메서드

async procesError( error ); // 에러 처리
getSequalize(); // 시퀄라이즈 얻기
isInitialize() // 초기화 확인 - 크게 의미는 없음 . 테스트용으로 제작

cf: src/test/UserTracking.spec.ts