在Ubuntu系统中使用Swagger进行监控与日志管理可以通过以下几种方法实现:
/etc/nginx/nginx.conf或/etc/nginx/sites-available/default)并添加或修改以下内容:server { ... access_log /var/log/nginx/swagger-ui-access.log; ... } /etc/apache2/sites-available/000-default.conf)并添加或修改以下内容:<VirtualHost *:80> ... CustomLog /var/log/apache2/swagger-ui-access.log combined ... </VirtualHost> logging模块配置日志记录。例如,在Flask应用中:import logging from flask import Flask app = Flask(__name__) logging.basicConfig(filename='app.log', level=logging.INFO) @app.route('/') def index(): app.logger.info('Index page requested') return 'Hello, World!' morgan中间件记录HTTP请求日志。例如:const express = require('express'); const morgan = require('morgan'); const app = express(); app.use(morgan('combined')); app.get('/', (req, res) => { console.log('Index page requested'); res.send('Hello, World!'); }); app.listen(3000, () => { console.log('Server is running on port 3000'); }); logback或log4j2等日志库记录日志。例如,在Spring Boot应用中:import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @SpringBootApplication @RestController public class Application { private static final Logger logger = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { SpringApplication.run(Application.class, args); } @GetMapping("/") public String index() { logger.info("Index page requested"); return "Hello, World!"; } } 使用logrotate管理日志文件:
sudo apt-get install logrotate /etc/logrotate.d/swagger-editor:/var/log/swagger-editor/*.log { daily missingok rotate 7 compress delaycompress notifempty create 0644 root root } sudo logrotate -d /etc/logrotate.d/swagger-editor # 测试配置文件是否有错误 sudo logrotate -f /etc/logrotate.d/swagger-editor # 强制旋转日志文件 sudo logrotate /etc/logrotate.d/swagger-editor 使用journalctl查看和管理日志:
swagger-editor的服务的日志:journalctl -u swagger-editor sudo journalctl --vacuum-time=1w # 只保留一周的日志 sudo journalctl --vacuum-size=500M # 只保留指定大小的日志 通过以上方法,你可以在Ubuntu上有效地监控和管理Swagger相关的日志文件。根据具体需求选择适合的方法进行日志管理。