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 🙏

© 2026 – Pkg Stats / Ryan Hefner

nodejs-log-agent

v1.0.2

Published

Modul Node.js untuk menyimpan log.

Readme

NodeJS Log Agent

nodejs-log-agent adalah modul logging komprehensif untuk aplikasi Node.js yang menyediakan penanganan log terpusat, layanan heartbeat, dan penanganan proses untuk graceful shutdown dan error handling global. Modul ini dirancang untuk menyederhanakan pelaporan status aplikasi dan informasi diagnostik.

Fitur

  • Logging Terstruktur: Memungkinkan log dengan level info, warn, dan error dengan format JSON yang terstruktur untuk kemudahan analisis.

  • Heartbeat Service: Mengirimkan log HEARTBEAT secara berkala untuk memverifikasi bahwa layanan backend berfungsi dengan baik.

  • Global Error Handling: Menginisialisasi listener untuk menangani exception yang tidak tertangkap (uncaughtException) dan penolakan promise yang tidak tertangkap (unhandledRejection), memastikan aplikasi tidak crash secara tiba-tiba dan log kesalahan tercatat.

  • Graceful Shutdown: Menangani sinyal proses seperti SIGTERM dan SIGINT untuk memungkinkan aplikasi membersihkan sumber daya dengan benar sebelum shutdown.

Instalasi

Untuk menginstal nodejs-log-agent, gunakan npm:

npm i nodejs-log-agent

Penggunaan

Modul ini menyediakan fungsionalitas logging inti, layanan heartbeat, dan inisialisasi penanganan proses.

Contoh app.js

Berikut adalah contoh bagaimana Anda dapat mengintegrasikan nodejs-log-agent ke dalam aplikasi Node.js Anda:

const { logger, heartbeatService, initializeProcessHandlers } = require('nodejs-log-agent');
const express = require('express'); // Contoh penggunaan Express
const app = express();
const port = process.env.PORT || 3000;

// --- Inisialisasi Penanganan Error Global Aplikasi dan Graceful Shutdown ---
// Panggil fungsi inisialisasi dari modul untuk mengaktifkan penanganan proses.
// Ini harus dipanggil sekali di awal aplikasi Anda.
initializeProcessHandlers(logger, heartbeatService);

// Contoh Route Sederhana
app.get('/', (req, res) => {
    logger.info('HOME_PAGE_ACCESS', { ip: req.ip, userAgent: req.headers['user-agent'], message: 'Home page accessed.' });
    res.send('Welcome to NodeJS Log Agent example!');
});

// Contoh Route untuk Mensimulasikan Error
app.get('/simulate-error', (req, res) => {
    try {
        // Simulasi error sinkron
        throw new Error('This is a simulated synchronous error!');
    } catch (err) {
        logger.error('SIMULATED_SYNC_ERROR_CAUGHT', {
            message: err.message,
            stack: err.stack,
            endpoint: '/simulate-error',
            context: 'Error sinkron tertangkap secara lokal.'
        });
        res.status(500).send('Simulated synchronous error caught and logged.');
    }
});

// Contoh Route untuk Mensimulasikan Warning
app.get('/simulate-warning', (req, res) => {
    logger.warn('SIMULATING_WARNING', { endpoint: '/simulate-warning', message: 'This is a simulated warning.' });
    res.status(200).send('Simulated warning logged.');
});

app.listen(port, () => {
    logger.info('APPLICATION_LISTENING', { message: `Backend server berjalan di http://localhost:${port}` });
    // Mulai layanan heartbeat setelah server mendengarkan
    heartbeatService.start();
});

// Contoh: Mensimulasikan unhandledRejection setelah beberapa detik (misalnya dari promise yang gagal)
setTimeout(() => {
    Promise.reject(new Error('This is an unhandled promise rejection!'));
}, 5000);

// Contoh: Mensimulasikan uncaughtException setelah beberapa detik
setTimeout(() => {
    // throw new Error('This is an uncaught exception!'); // Aktifkan ini untuk menguji uncaughtException
}, 10000);

Fungsi Logging

Modul ini menyediakan metode logging yang mudah digunakan:

logger.info(code, data)

Digunakan untuk mencatat informasi umum atau kejadian penting dalam aplikasi.

  • code (string): Kode identifikasi unik untuk jenis log (misalnya, APPLICATION_STARTED, REQUEST_SUCCESS).

  • data (object): Objek yang berisi data relevan terkait log, seperti pesan, parameter, atau hasil operasi.

Contoh:

logger.info('APPLICATION_LISTENING', { message: `Backend server berjalan di http://localhost:${port}` });

logger.warn(code, data)

Digunakan untuk mencatat peringatan yang menunjukkan potensi masalah atau situasi yang tidak ideal, namun tidak menyebabkan kegagalan aplikasi.

  • code (string): Kode identifikasi unik untuk jenis peringatan.

  • data (object): Objek yang berisi data relevan terkait peringatan.

Contoh:

logger.warn('SIMULATING_WARNING', { endpoint: '/simulate-error', message: 'This is a simulated warning.' });

logger.error(code, data)

Digunakan untuk mencatat kesalahan fatal atau exception yang memerlukan perhatian.

  • code (string): Kode identifikasi unik untuk jenis kesalahan.

  • data (object): Objek yang berisi data kesalahan, seperti message, stack trace, dan konteks tambahan.

Contoh:

logger.error('SIMULATED_SYNC_ERROR_CAUGHT', {
    message: err.message,
    stack: err.stack,
    endpoint: '/simulate-error',
    context: 'Error sinkron tertangkap secara lokal.'
});

Dengan nodejs-log-agent, Anda dapat mengelola log aplikasi Anda dengan lebih efisien, memastikan visibilitas yang lebih baik terhadap status dan kesehatan sistem Anda.