Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
20
rated 0 times [  22] [ 2]  / answers: 1 / hits: 38087  / 9 Years ago, sat, january 9, 2016, 12:00:00

In my node.js project I use log4js and I want my logs to have name of the file where log record was added. So I use __filename but it gives me absolute path



var logger = log4js.getLogger(__filename)


So it gives me logs like this:



[1999-01-01 00:00:00] [DEBUG] /Users/whatever/myproject/src/services/users something happened


But I want the path to be relative to my project/src folder. Like this:



[1999-01-01 00:00:00] [DEBUG] services/users something happened


What is the best way to achieve this?


More From » node.js

 Answers
35

You can use path.relative:



var relativePath = path.relative(process.cwd(), someFilePath);

[#63789] Thursday, January 7, 2016, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
clarkulisesa

Total Points: 422
Total Questions: 93
Total Answers: 112

Location: Austria
Member since Thu, Jan 7, 2021
3 Years ago
clarkulisesa questions
Mon, Feb 24, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;