Home Database Mysql Tutorial MongoDB做日志服务器

MongoDB做日志服务器

Jun 07, 2016 pm 02:53 PM
mongodb log server

MongoDB做日志服务器 MongoDB 做日志服务器。 www.2cto.com 写日志最常用的方式是写入纯文本文件,然后安日期分割文件,压错旧文件。 这样的日志与对懂shell,perl语言的人分析起来非常方便,对于程序员来说还是更喜欢写入数据库服务器,然后通过sql语句查询

MongoDB做日志服务器

 

MongoDB 做日志服务器。

   www.2cto.com  

写日志最常用的方式是写入纯文本文件,然后安日期分割文件,压错旧文件。

 

这样的日志与对懂shell,perl语言的人分析起来非常方便,对于程序员来说还是更喜欢写入数据库服务器,然后通过sql语句查询。

对于程序员来说sql语句提供了丰富查询功能,相比文本文件分析更容易。

 

下面就是一个简单日志表, 尽量做到通用所以只有message。

 

MongoDB做日志服务器

 

Sql代码  

CREATE TABLE `logging` (  

    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,  

    `tag` ENUM('unknow','user','bbs','cart','admin') NOT NULL DEFAULT 'unknow' COMMENT '日志标签域',  

    `asctime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '产生时间',  

    `facility` ENUM('unknow','account','bank','unionpay','sms','email','register') NOT NULL DEFAULT 'unknow' COMMENT '类别',  

    `priority` ENUM('info','warning','error','critical','exception','debug') NOT NULL DEFAULT 'debug' COMMENT '级别',  

    `message` VARCHAR(512) NOT NULL COMMENT '内容',  

    `operator` VARCHAR(50) NOT NULL DEFAULT 'computer' COMMENT '操作者',  

    PRIMARY KEY (`id`)  

)  

COMMENT='日志表'  

COLLATE='utf8_general_ci'  

ENGINE=InnoDB;  

   www.2cto.com  

 

mongodb 的message字段比起sql更灵活

 

Java代码  

db.logging.user.save({'asctime':'2012-10-10 12:12:12','facility':'register','priority':'info','operator':'computer','message':{'name':'neo','address':{'city':'shenzhen','post':518000},'phone':[13113668890,13322993040]}})  

db.logging.user.save({'asctime':'2012-10-10 12:12:12','facility':'sms','priority':'error','operator':'computer','message':'send sms: 13113668890,text: xxxxx'})  

  

db.logging.user.find()  

  

db.logging.admin.save({'asctime':'2012-10-10 12:12:12','facility':'account','priority':'info','operator':'computer','message':'delete account'})  

db.logging.admin.save({'asctime':'2012-10-10 12:12:12','facility':'sms','priority':'info','operator':'computer','message':'send sms'})  

db.logging.admin.save({'asctime':'2012-10-10 12:12:12','facility':'bank','priority':'warning','operator':'computer','message':'bank from xxxx to xxxx'})  

  

db.logging.admin.find()  

 

Java代码  

> db.logging.user.find()  

{ "_id" : ObjectId("50cc10dd3e4f5a2b92fb5f37"), "asctime" : "2012-10-10 12:12:12", "facility" : "register", "priority" : "info", "operator" : "computer", "message" : { "name" : "neo", "address" : { "city" : "shenzhen", "post" : 518000 }, "phone" : [ 13113668890, 13322993040 ] } }  

{ "_id" : ObjectId("50cc11a23e4f5a2b92fb5f39"), "asctime" : "2012-10-10 12:12:12", "facility" : "sms", "priority" : "error", "operator" : "computer", "message" : "send sms: 13113668890" }  

> db.logging.admin.find()  

{ "_id" : ObjectId("50cc11443e4f5a2b92fb5f38"), "asctime" : "2012-10-10 12:12:12", "facility" : "account", "priority" : "info", "operator" : "computer", "message" : "delete account" }  

{ "_id" : ObjectId("50cc120c3e4f5a2b92fb5f3a"), "asctime" : "2012-10-10 12:12:12", "facility" : "bank", "priority" : "warning", "operator" : "computer", "message" : "bank from xxxx to xxxx" }  

>  

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Equipped with AMD EPYC 4004 series processors, ASUS launches a variety of server and workstation products Equipped with AMD EPYC 4004 series processors, ASUS launches a variety of server and workstation products Jul 23, 2024 pm 09:34 PM

According to news from this website on July 23, ASUS has launched a variety of server and workstation-level products powered by AMD EPYC 4004 series processors. Note from this site: AMD launched the AM5 platform and Zen4 architecture EPYC 4004 series processors in May, offering up to 16-core 3DV-Cache specifications. ASUSProER100AB6 server ASUSProER100AB6 is a 1U rack server product equipped with EPYC Xiaolong 4004 series processor, suitable for the needs of IDC and small and medium-sized enterprises. ASUSExpertCenterProET500AB6 workstation ASUSExpertCenterProET500AB6 is a

Performance optimization tips for logging mechanism in Java functions? Performance optimization tips for logging mechanism in Java functions? May 02, 2024 am 08:06 AM

Logging optimization tip: Disable debug logging to eliminate the impact. Batch log messages to reduce overhead. Use asynchronous logging to offload logging operations. Limit log file size to improve application startup and processing performance.

What is the use of net4.0 What is the use of net4.0 May 10, 2024 am 01:09 AM

.NET 4.0 is used to create a variety of applications and it provides application developers with rich features including: object-oriented programming, flexibility, powerful architecture, cloud computing integration, performance optimization, extensive libraries, security, Scalability, data access, and mobile development support.

How to ensure high availability of MongoDB on Debian How to ensure high availability of MongoDB on Debian Apr 02, 2025 am 07:21 AM

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

How to configure MongoDB automatic expansion on Debian How to configure MongoDB automatic expansion on Debian Apr 02, 2025 am 07:36 AM

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

How does the Java framework support monitoring and logging in microservices architecture? How does the Java framework support monitoring and logging in microservices architecture? Jun 04, 2024 pm 12:26 PM

The Java framework supports monitoring and logging in microservices architecture through: Monitoring support: Provides built-in monitoring endpoints such as SpringBootActuator to collect application status and performance metrics. Logging integration: Integrate with third-party logging libraries such as Log4j and Logback to provide rich logging functions. Practical case: Integrating ElasticsearchStack can be integrated with ElasticsearchStack to provide advanced monitoring and logging capabilities, such as centralized storage and analysis.

Major update of Pi Coin: Pi Bank is coming! Major update of Pi Coin: Pi Bank is coming! Mar 03, 2025 pm 06:18 PM

PiNetwork is about to launch PiBank, a revolutionary mobile banking platform! PiNetwork today released a major update on Elmahrosa (Face) PIMISRBank, referred to as PiBank, which perfectly integrates traditional banking services with PiNetwork cryptocurrency functions to realize the atomic exchange of fiat currencies and cryptocurrencies (supports the swap between fiat currencies such as the US dollar, euro, and Indonesian rupiah with cryptocurrencies such as PiCoin, USDT, and USDC). What is the charm of PiBank? Let's find out! PiBank's main functions: One-stop management of bank accounts and cryptocurrency assets. Support real-time transactions and adopt biospecies

Logging and monitoring strategies in PHP framework Logging and monitoring strategies in PHP framework Jun 01, 2024 am 11:17 AM

Best practices for logging and monitoring in PHP frameworks: Logging: Choose a PSR-3 compliant logging library, define log levels, configure log processors, and use contextual information. Monitoring: Integrate APM tools, set metrics and alerts, enable tracing, and collect errors and exceptions. Best practices: Automate logging and monitoring, use consistent naming conventions, adjust log levels appropriately, and review logs regularly.

See all articles