โปรเจคตัวอย่างสำหรับการใช้งาน NestJS พร้อมกับ Loki logging system และ Swagger API documentation
- NestJS Framework - Modern Node.js framework
- Loki Logging - Centralized logging system
- Swagger/OpenAPI - Interactive API documentation
- TypeScript - Full type safety
- Validation - Request validation with class-validator
- Structured Logging - Advanced logging capabilities
- Node.js 18+
- npm หรือ yarn
- Docker (สำหรับ Loki)
# Clone repository git clone <repository-url> cd nest-loki-log-sample # Install dependencies npm install # Copy environment file cp .env.example .env # Edit environment variables nano .env
สร้างไฟล์ .env ในโฟลเดอร์หลัก:
# Application Configuration NODE_ENV=development PORT=4000 APP_NAME=nest-loki-logger # Logging Configuration LOG_LEVEL=info # Loki Configuration LOKI_HOST=localhost LOKI_PORT=3100 LOKI_PROTOCOL=http LOKI_USERNAME= LOKI_PASSWORD= # Swagger Configuration SWAGGER_TITLE=NestJS Loki Logger API SWAGGER_DESCRIPTION=API สำหรับการทดสอบ NestJS พร้อมกับ Loki logging system SWAGGER_VERSION=1.0
# Start Loki with Docker Compose npm run loki:up # Check logs npm run loki:logs # Stop Loki npm run loki:down
# Development mode npm run start:dev # Production build npm run build npm run start:prod # Debug mode npm run start:debug
หลังจากรันแอปพลิเคชันแล้ว สามารถเข้าถึง Swagger UI ได้ที่:
URL: http://localhost:4000/api
GET /- Root endpoint
GET /example- Hello endpointGET /example/performance- Performance testPOST /example/users- Create userGET /example/users/:id- Get user by IDGET /example/error- Trigger error testGET /example/structured- Structured logging test
# Unit tests npm run test # E2E tests npm run e2e # Test coverage npm run test:cov # Test logging functionality npm run test:logging
โปรเจคนี้มีตัวอย่างการใช้งาน logging หลายรูปแบบ:
this.logger.log('Message', 'Context'); this.logger.error('Error message', 'stack trace', 'Context'); this.logger.warn('Warning message', 'Context');
this.logger.logPerformance('operation_name', duration, 'Context');
this.logger.logBusinessEvent('user_created', userData, 'Context');
this.logger.logWithLabels('Message', customLabels, 'Context');
src/
├── config/ # Configuration files
├── controllers/ # API controllers
├── dto/ # Data Transfer Objects
├── modules/ # Feature modules
├── services/ # Business logic services
└── main.ts # Application entry point
- สร้าง DTO ใน
src/dto/ - เพิ่ม Swagger decorators
- สร้าง controller method
- อัปเดต Swagger documentation
import { ApiProperty } from '@nestjs/swagger'; import { IsString, IsNotEmpty } from 'class-validator'; export class NewDto { @ApiProperty({ description: 'Description', example: 'Example value' }) @IsString() @IsNotEmpty() field: string; }
- Loki: http://localhost:3100
- Application: http://localhost:4000
- API Docs: http://localhost:4000/api
- Fork the repository
- Create feature branch
- Commit changes
- Push to branch
- Create Pull Request
This project is licensed under the MIT License.