MongoDB as a log server

Source: Internet
Author: User
MongoDB is the log server. The most common way to write logs at www.2cto.com is to write a plain text file, and then install the date to split the file and press the wrong old file. This log is very convenient for anyone who understands shell and perl. For programmers, they prefer to write logs to database servers and then use SQL statements to query logs.

MongoDB is the log server. The most common way to write logs at www.2cto.com is to write a plain text file, and then install the date to split the file and press the wrong old file. This log is very convenient for anyone who understands shell and perl. For programmers, they prefer to write logs to database servers and then use SQL statements to query logs.

MongoDB as a log server

MongoDB is used as the log server.

Www.2cto.com

The most common way to write logs is to write plain text files, and then install the date to split the files and press the wrong old files.

This log is very convenient for anyone who understands shell and perl. For programmers, they prefer to write data to database servers and then query data using SQL statements.

For programmers, SQL statements provide rich query functions, making it easier to analyze text files.

The following is a simple log table, which is as common as possible, so there is only message.

SQL code

Create table 'logging '(

'Id' INT (10) unsigned not null AUTO_INCREMENT,

'Tag' ENUM ('unknow', 'user', 'bbs ', 'cart', 'admin') not null default 'unknow' comment' log tag Domain ',

'Asctime' timestamp not null default CURRENT_TIMESTAMP comment' generation time ',

'Facility 'enum ('unknow', 'account', 'bank', 'unionpa', 'ms', 'email ', 'Register ') not null default 'unknow' comment' category ',

'Priority 'enum ('info', 'warning', 'error', 'critical ', 'exception', 'debug') not null default 'debug' comment' level ',

'Message' VARCHAR (512) not null comment' content ',

'Operator' VARCHAR (50) not null default 'computer 'comment' operator ',

Primary key ('id ')

)

COMMENT = 'Log table'

COLLATE = 'utf8 _ general_ci'

ENGINE = InnoDB;

Www.2cto.com

Mongodb's message field is more flexible than SQL

Java code

Db. logging. user. save ({'asctime': '2017-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': '2017-10-10 12:12:12 ', 'facility': 'ms', 'priority ': 'error', 'operator': 'computer ', 'message': 'send sms: 13113668890, text: xxxxx '})

Db. logging. user. find ()

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

Db. logging. admin. save ({'asctime': '2017-10-10 12:12:12 ', 'facility': 'ms', 'priority ': 'info', 'operator': 'computer ', 'message': 'send Ms '})

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

Db. logging. admin. find ()

Java code

> Db. logging. user. find ()

{"_ Id": ObjectId ("50cc10dd3e4f5a2b92fb5f37"), "asctime": "12:12:12", "facility": "register", "priority": "info ", "operator": "computer", "message": {"name": "neo", "address": {"city": "shenzhen", "post ": 518000}, "phone": [13113668890,133 22993040]}

{"_ Id": ObjectId ("50cc11a23e4f5a2b92fb5f39"), "asctime": "12:12:12", "facility": "sms", "priority": "error ", "operator": "computer", "message": "send sms: 13113668890 "}

> Db. logging. admin. find ()

{"_ Id": ObjectId ("50cc11443e4f5a2b92fb5f38"), "asctime": "12:12:12", "facility": "account", "priority": "info ", "operator": "computer", "message": "delete account "}

{"_ Id": ObjectId ("50cc120c3e4f5a2b92fb5f3a"), "asctime": "12:12:12", "facility": "bank", "priority": "warning ", "operator": "computer", "message": "bank from xxxx to xxxx "}

>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.