136

I'm trying to Append data to a Log file using Node.js and that is working fine but it is not going to the next line. \n doesn't seem to be working in my function below. Any suggestions?

function processInput ( text ) 
{     
  fs.open('H://log.txt', 'a', 666, function( e, id ) {
   fs.write( id, text + "\n", null, 'utf8', function(){
    fs.close(id, function(){
     console.log('file is updated');
    });
   });
  });
 }
1
  • 4
    Are you on Windows/using a Windows-based text editor to view your file, and thus need a CRLF pair, \r\n? Commented Apr 30, 2012 at 13:17

5 Answers 5

207

It looks like you're running this on Windows (given your H://log.txt file path).

Try using \r\n instead of just \n.

Honestly, \n is fine; you're probably viewing the log file in notepad or something else that doesn't render non-Windows newlines. Try opening it in a different viewer/editor (e.g. Wordpad).

Sign up to request clarification or add additional context in comments.

2 Comments

was viewing in notepad :)
FYI in Notepad++ you can do find all "\\n" replace "\n" with "Extended" search mode selected at the bottom of the dialog.
135

Use the os.EOL constant instead.

var os = require("os");

function processInput ( text ) 
{     
  fs.open('H://log.txt', 'a', 666, function( e, id ) {
   fs.write( id, text + os.EOL, null, 'utf8', function(){
    fs.close(id, function(){
     console.log('file is updated');
    });
   });
  });
 }

7 Comments

(1/2) Please note that since node.js can run on many different environments, it may be possible to move your application from lets say a windows environment to a linux one. that means if your application is appending logs using os.EOL you will have some lines ending with /r/n (from the time app was running on windows) and then you will have rows ending just with /n (when app runs on linux). This has the potential to give some trouble especially if any automatic parsing of log files is in place. I decided to just use /n.
(2/2) Nevertheless i upvoted your answer as it would be the correct one, if we lived in a perfect-regulated world.
I use linux but liked this approach as I might want portability in future use.
This is the best answer because it works crossplatform.
Well, I had to package an application that used fs.write with os.EOL into a binary. I used pkg to generate a binary for Windows. When executed, the last written record has a new line appended to it (as expected); but, seems like other Windows applications treat it is a new record. I am hunting for a work-around.
|
12

use \r\n combination to append a new line in node js

  var stream = fs.createWriteStream("udp-stream.log", {'flags': 'a'});
  stream.once('open', function(fd) {
    stream.write(msg+"\r\n");
  });

Comments

5

Alternatively, you can use fs.appendFile method

let content = 'some text';
content += "\n";
fs.appendFile("helloworld.txt", content, (err) => {
    return console.log(err);
});

2 Comments

how is this useful?
@LakshmanPilaka less code, async
-3

Try:

var fs =require('fs');

const details=require('./common');
var info=JSON.stringify(details);

const data=fs.writeFileSync('./tmp/hello/f1.txt',`\n${info}`,{'flag':'a'},function(err,data){
    
if(err) return console.error("error",error);
    console.log(data);

});

//steps to exceute 1.Install all the required modules(ie fs is required here). 2.Here (.common) files has json object which i was importing from another file. 3.then import the file and store in details variable. 4.While performing operations json data has to be converted into string format (so JSON.stringify). 5.WriteFileSync (its an synchronous function) 6.once function execution is completed response is returned. 7.store response in data variable and print in console.log

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.