How do I write logs to filesystem from NodeJS?

I have been trying to use Winston or Bunyan loggers with a NodeJS proxy. However I am unable to write out a log file and can only write to console which eventually get amalgamated within the MP logs.

I'd like to keep the Node logs separate so that it is easier to read / trace at a later point.

We are using the OPDK version and we have been trying all possible file system permissions but are unable to write any file out of Edge to the filesystem.

I can across this in the docs -> "Filesystem access is restricted to the directory where the script was launched: the /resources/nodedirectory. Node.js scripts may read and write files within this directory, for instance as a temporary scratch area, but there are no guarantees as to how long the files will persist."

Link : http://apigee.com/docs/api-services/content/understanding-edge-support-nodejs-modules

Does this mean that *absolutely* nothing can be done to be able to write files to the file system on the MP / MS?

1 2 1,060
2 REPLIES 2

Not applicable

Hi @Prashanth Subrahmanyam. Good question. I did some experimentation logging with Node.js on Edge and perhaps you read this article already, API Log Management - Push Model. I understand you're trying to read from the filesystem the log files. I believe an API that may become handy to you is Cached Logs from the Management API, and although it is not necessarily exposed as a file, you can retrieve entries from it from an external agent in a pull fashion. I've done this in the past with OPDK customer leveraging Splunk forwarders or agents accessing Cached Logs API remotely.

Is this something workable for your implementation?

Good answer -- the "cached logs" feature of the API (as well as the "apigeetool getlogs" command) will return to you what you write to standard output or standard error. So one approach is to just do that.

Otherwise, I have heard of some customers using various Node.js logging packages to write logs to a server outside the Apigee cloud.

And yes, you can write to and read from the filesystem. But you should consider the "filesystem" that your Node app sees to be a virtual filesystem that is not readable or writable from outside your node app, and one that you cannot count on being around for a long time or after your app is stopped.