Spaces:
Sleeping

File size: 2,106 Bytes
0b355ec
 
 
 
 
 
 
 
 
 
 
 
61a2a4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b355ec
61a2a4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b355ec
 
 
 
 
 
 
 
 
 
 
 
61a2a4f
 
 
 
 
 
 
0b355ec
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const winston = require('winston');

// Define log format
const logFormat = winston.format.combine(
  winston.format.timestamp({
    format: 'YYYY-MM-DD HH:mm:ss'
  }),
  winston.format.errors({ stack: true }),
  winston.format.json(),
  winston.format.prettyPrint()
);

// Create logs directory if it doesn't exist (before creating logger)
const fs = require('fs');
const path = require('path');
const logsDir = path.join(process.cwd(), 'logs');

let useFileLogging = true;
try {
  if (!fs.existsSync(logsDir)) {
    fs.mkdirSync(logsDir, { recursive: true });
  }
} catch (error) {
  console.warn('Cannot create logs directory, using console logging only:', error.message);
  useFileLogging = false;
}

// Create transports array
const transports = [];

// Add file transports only if we can write to filesystem
if (useFileLogging) {
  try {
    transports.push(
      new winston.transports.File({
        filename: 'logs/error.log',
        level: 'error',
        maxsize: 5242880, // 5MB
        maxFiles: 5,
        tailable: true
      }),
      new winston.transports.File({
        filename: 'logs/combined.log',
        maxsize: 5242880, // 5MB
        maxFiles: 5,
        tailable: true
      })
    );
  } catch (error) {
    console.warn('Cannot create file transports, using console logging only:', error.message);
    useFileLogging = false;
  }
}

// Always add console logging in production if file logging fails
if (!useFileLogging || process.env.NODE_ENV === 'production') {
  transports.push(new winston.transports.Console({
    format: winston.format.combine(
      winston.format.colorize(),
      winston.format.simple(),
      winston.format.printf(({ timestamp, level, message, ...meta }) => {
        return `${timestamp} [${level}]: ${message} ${
          Object.keys(meta).length ? JSON.stringify(meta, null, 2) : ''
        }`;
      })
    )
  }));
}

// Create the logger
const logger = winston.createLogger({
  level: process.env.LOG_LEVEL || 'info',
  format: logFormat,
  defaultMeta: { service: 'uga-chatbot' },
  transports: transports,
});


module.exports = logger;