Having trouble with log4js-node in Node.js not recording logs to file?

Not a expert in nodes, this is my first time using log4js-node.

I am attempting to log my ERROR logs and any console logs to a file named log_file.log using log4js on a nodejs server running Express. Below is my configuration file:

{
  "replaceConsole": true,
  "appenders": [
  {
    "type": "file",
    "filename":"log_file.log",
    "maxLogSize":20480,
    "backups": 3,
    "category":"relative-logger"
  },
  {
  "type":"logLevelFilter",
  "level":"ERROR",
  "appender":{
    "type":"file",
    "filename":"log_file.log"
    }
  },
  {

    "appender": {
      "type": "smtp",
      "recipients": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2cfdbc7cfc3cbcee2c5cfc3cbce8cc1cdcf">[email protected]</a>",
      "sender": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84e9fde1e9e5ede8e5e0e0c4e3e9e5ede8aae7ebe9">[email protected]</a>",
      "sendInterval": 60,
      "transport": "SMTP",
      "SMTP": {
        "host": "localhost",
        "port": 25
      }
    }
  }]
}`

This is how I'm including the application in my app.js file:

var log4js = require("log4js");
log4js.configure("log_config.json")
logger = log4js.getLogger();

I'm manually sending errors to log4js like this (able to log to console but not to the log_file):

logger.error('A mandrill error occurred: ' + e.name + ' - ' + e.message);

Additionally, I want log4js to capture the application's standard ERROR messages.

How can I configure log4js to log to the log_file.log and then email me that log? Note that I have nodemailer 0.7 installed for smtp handling.

Answer №1

Consider deleting the line "category":"relative-logger" from your appender file.

Answer №2

Remove the line "category":"relative-logger" as it seems to be causing a blockage in transferring data to your log file. Alternatively, you can try configuring the logging setup like this:

// Configure Logging
log4js.configure({
    appenders: [
        { type: 'console' },
        { type: 'file', filename: '.\\logs\\PesaFastaArchiveData.log' }
    ]
});

Keep in mind that the path provided is specific to Windows.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Vue.js Enhances CoolLightBox with Multiple Galleries

I am trying to set up a page with multiple galleries using CoolLightBox. It worked fine for me when I had just one gallery, but now that I want to create multiple galleries - each with its own image on the page - it is only displaying one image in the ligh ...

In this situation where the component is being utilized as {Component} instead of <Component/>, how can I effectively pass and retrieve props?

I'm just starting to learn about React and I am trying to understand how to pass props from NavItemsLayout. const NavItemsLayout = (props)=>{ return( <div className="nav-items"> Hello, World! </div> ) } ...

Trigger a React event using D3.js dispatch

While working with my axis, I encountered an issue when trying to dispatch a React event along with a payload. What I noticed was that when I used console.log('item'), a pointer event was being logged instead of the expected item property. Is thi ...

Is it possible to preserve the <canvas> elements to use for future animation frames?

Currently, I am working on a graph visualization project that involves complex calculations being done on an HTML5 canvas. My goal is to create an interactive animation where the graph remains fixed, but additional elements are overlaid as the mouse moves ...

What is the best way to customize a button component's className when importing it into another component?

Looking to customize a button based on the specific page it's imported on? Let's dive into my button component code: import React from "react"; import "./Button.css"; export interface Props { // List of props here } // Button component def ...

Error: AJAX response shows as NaN, indicating that the requested resource was not found

Attempting to create a search engine using AJAX. When typing in the search box, nothing happens. After inspecting the element and opening the console, an error message is displayed: script.js:19 GET http://localhost/var/www/html/pendaftaran-siswa/NaN 404 ( ...

Generating an Audio element on the fly using Javascript

I am looking to dynamically generate an audio tag within the <div class="audio-player" id="song"></div> section. I require: <audio id="audio-player" controls="controls" src="media/Blue Browne.mp3" type="audio/mpeg"> to be placed insid ...

NodeJs Express authentication with passport-jwt returning incorrect user data from token

I recently implemented an authentication API on my Node.js server, utilizing Passportjs as my middleware and passport-jwt as my strategy. Here is a snippet from my passport.js file let JwtStrategy = require("passport-jwt").Strategy, ExtractJwt = require ...

What is the reason behind the necessity of using setTimeout with a delay of at least 50ms for JS .focus()

Problem While creating a page for a client at work, I encountered an issue with a slide-out search bar. When clicking the button to open the search input field (which starts off hidden), I want the focus to shift to that input field. Oddly, I found that ...

"Integrating multiple partials with templateUrl in AngularJS: A step-by-step

Is there a way to merge partial views using the templateUrl attribute in an AngularJS directive? Imagine having a directive like this: .directive('combinePartials', function () { var mainPartial = "mainpartial.html", var template2 = "pa ...

What is the best way to style a tooltip in an ASP.NET application using CSS?

protected void grd_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { foreach (TableCell cell in e.Row.Cells) { ...

What is the best way to define file paths in a webpage to ensure that the same file works seamlessly on both server and

Currently, I am working on developing a website locally with the intention of later transferring it via FTP to my server. In my index.php file, there is a line that reads: <?php include($_SERVER['DOCUMENT_ROOT'] . "/includes/header.php");?&g ...

Leveraging bootstrap's grid system to prioritize content layout based on the

Currently working on a compact React Js project with Bootstrap for styling. One obstacle I've encountered is the need to display the last element as the first one on the page. To achieve this, I plan to utilize the order-1 and order-12 classes from Bo ...

Solution to trigger CSS :hover to refresh following a transition (such as expanding a menu)

While there are existing discussions on this topic, I am presenting my query for two specific reasons: It introduces a potential alternative solution The demo code could be helpful to individuals looking to emulate a menu Following a CSS transition, the ...

Crafting Effective AngularJS Directives

Recently, I've been delving into AngularJS and have grasped the core concepts quite well. As I embark on building my own application, I find myself struggling with laying out the blueprint, particularly in terms of directive design. Are there any not ...

Need to find a way for the function to wait for the response before returning, as the XMLHttpRequest GET function currently returns after sending the request

I'm currently facing an issue with fetching an array from my mongodb database and setting it to a variable that I can use in another function. The problem is that when I call the getFromAPI() method from another function, the function returns after th ...

What is the correct way to display the date in a React application?

I am working on a project that involves integrating solidity contracts into a web portal. In one of the contracts, I have stored dates as uint values like this: 1539491531. However, when I display these dates on the web page, they appear different from wh ...

Unable to access the value of an undefined property

In my HTML view, I have the following code: <form name="adduser" id="signupForm" novalidate> <span>First Name</span> <label class="item item-input" id="profileLabel"> <input id="profileInput" type="text" ...

Leveraging AngularJS ngBind with a JavaScript object

Within the given string, integrating a javascript object and embedding it into an ngBinding does not result in proper evaluation. I have a string where I want to incorporate a specific part of a javascript object and am transitioning to Angular for its use ...

Error: The 'encoding' property cannot be set on null objects in THREE.js

I have been trying to load a basic grass block from Minecraft, but I keep encountering an error message that says TypeError: Cannot set properties of null (setting 'encoding'). This error originates from line 2893 in GLTFLoader, specifically the ...