前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >swift vapor 简易在线日志统计

swift vapor 简易在线日志统计

作者头像
大话swift
发布2019-07-04 11:08:40
6960
发布2019-07-04 11:08:40
举报
文章被收录于专栏:大话swift大话swift

表结构:

struct LOUser: MySQLModel {

var id: Int?

// 当前登陆人标志

var identifier: String

init(identifier: String) {

self.identifier = identifier

}

}

struct LOUserDevicePivot: MySQLPivot {

static var leftIDKey: WritableKeyPath<LOUserDevicePivot, Int> = \.userId

static var rightIDKey: WritableKeyPath<LOUserDevicePivot, Int> = \.deviceId

typealias Left = LOUser

typealias Right = LODevice

var id: Int?

var userId: Int

var deviceId: Int

init(userId uId: Int, deviceId dId: Int) {

self.userId = uId

self.deviceId = dId

}

}

struct LOLog: MySQLModel {

var id: Int?

/// 用户 和 device的映射

var uDevicePivotId: Int

var groupId: Int

var shortURL: String

var query: String

var responseBody:Data

var mode: LogMode = .debug

var level: LogLevel = .info

init( groupId: Int, uDevicePivotId: Int, shortURL: String, query: String, responseBody: String, mode:LogMode = LogMode.debug, level: LogLevel = LogLevel.info) {

self.groupId = groupId

self.uDevicePivotId = uDevicePivotId

self.query = query

self.shortURL = shortURL

self.responseBody = responseBody.data(using: String.Encoding.utf8

)!

self.mode = mode

self.level = level

}

init() {

self.init(groupId: 0, uDevicePivotId: 0,shortURL:"", query: "", responseBody: "")

}

}

struct LOGroupUserPivot: MySQLPivot{

static var leftIDKey: WritableKeyPath<LOGroupUserPivot, Int> = \.userId

static var rightIDKey: WritableKeyPath<LOGroupUserPivot, Int> = \.groupId

typealias Left = LOUser

typealias Right = LOGroup

var id: Int?

var userId: Int

var groupId: Int

init(user userId: Int, group groupId: Int) {

self.userId = userId

self.groupId = groupId

}

}

struct LOGroup: MySQLModel {

var id: Int?

var name: String

//推荐添加bundle,默认使用时间戳

var bundle: String?

var idetifier: String?

var createTime: TimeInterval?

init(name nickName: String, bundle: String? = "\(Date().timeIntervalSince1970)" ) {

self.name = nickName

self.idetifier = "\(Date().timeIntervalSince1970)"

createTime = Date().timeIntervalSince1970

}

}

struct LODeviceRequest: Content {

var uuid: String

var deviceJsonInfo: String

init(uuid: String,info deviceJsonInfo: String) {

self.uuid = uuid

self.deviceJsonInfo = deviceJsonInfo

}

public static var defaultContentType: MediaType {

return MediaType.formData

}

}

struct LODevice: MySQLModel {

var id: Int?

var uuid: String

var deviceJsonInfo: String

init(uuid: String,info deviceJsonInfo: String) {

self.uuid = uuid

self.deviceJsonInfo = deviceJsonInfo

}

}

数据库连接、升级

func config_db(database: inout DatabasesConfig) -> MySQLDatabase{

#if os(macOS)

let hostname = "127.0.0.1"

let password = "12345678"

#else

let hostname = "192.168.3.61"

let password = "123456"

#endif

let mysqlConfig : MySQLDatabaseConfig = MySQLDatabaseConfig.init(

hostname: hostname,

port: 3306,

username: "root",

password: password,

database: "LogOnline",

capabilities: .default,

characterSet: .utf8mb4_unicode_ci,

transport: .cleartext

)

let mysqlDb: MySQLDatabase = MySQLDatabase.init(config: mysqlConfig)

defer {

database.disableReferences(on: .mysql)

}

database.add(database: mysqlDb, as: DatabaseIdentifier<MySQLDatabase>.mysql)

return mysqlDb

}

func config_migrations(migrations:inout MigrationConfig){

migrations.add(model: LOUser.self, database: .mysql)

migrations.add(model: LODevice.self, database: .mysql)

migrations.add(model: LOGroup.self, database: .mysql)

migrations.add(model: LOGroupUserPivot.self, database: .mysql)

migrations.add(model: LOLog.self, database: .mysql)

migrations.add(model: LOUserDevicePivot.self, database: .mysql)

migrations.add(model: CacheEntry<MySQLDatabase>.self, database: .mysql)

}

SPM兼容Linux/macOS处理 采用预编译

#if os(macOS)

// macOS

let package = Package(

name: "LogOnline",

dependencies: [

// ? A server-side Swift web framework.

.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),

.package(url: "https://github.com/vapor/fluent-mysql.git", from: "3.0.1"),

.package(url: "https://github.com/vapor/auth.git", from: "2.0.0"),

.package(url: "https://github.com/vapor/leaf.git", from: "3.0.1"),

.package(url: "https://github.com/Alamofire/Alamofire.git", from: "4.0.0"),

// ? Swift ORM (queries, models, relations, etc) built on SQLite 3.

.package(url: "https://github.com/vapor/fluent-sqlite.git", from: "3.0.0")

],

targets: [

.target(name: "App", dependencies: ["Leaf","Authentication","FluentSQLite","FluentMySQL", "Vapor"]),

.target(name: "Run", dependencies: ["App"]),

.testTarget(name: "AppTests", dependencies: ["App","Alamofire"])

]

)

#else

// Linux

let package = Package(

name: "LogOnline",

dependencies: [

// ? A server-side Swift web framework.

.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),

.package(url: "https://github.com/vapor/fluent-mysql.git", from: "3.0.1"),

.package(url: "https://github.com/vapor/auth.git", from: "2.0.0"),

.package(url: "https://github.com/vapor/leaf.git", from: "3.0.1"),

// ? Swift ORM (queries, models, relations, etc) built on SQLite 3.

.package(url: "https://github.com/vapor/fluent-sqlite.git", from: "3.0.0")

],

targets: [

.target(name: "App", dependencies: ["Leaf","Authentication","FluentSQLite","FluentMySQL", "Vapor"]),

.target(name: "Run", dependencies: ["App"]),

.testTarget(name: "AppTests", dependencies: ["App" ])

]

)

#endif

日志统计

日志等级分为: info warn error

环境分为: debug product

public enum LogMode: Int{

case debug

case product

}

public enum LogLevel: Int{

case info

case error

case warn

}

部署环境:

Ubuntu 14

vapor

mysql代码管理:Git同步

代码托管: GitHub1

Git同步git clone git@github.com:skeyboy/LogOnline.git

2 编译cd ~/LogOnline

vapor build

3 启动服务

vapor run //可以看日志

nohup vapor run & //需要两个回车键,让后端运行

4 退出ssh连接

exit //需要回车

用户登录(因为内部,自动注册人员

日志大纲浏览

/log/scan?uDevicePivotId=1&groupId=1&mode=0&level=1&pno=1&max=10

日志详情

/log/detail?logId=20

POSTMan压力测试

响应大概是0.4s左右

后续改进

1 邮件通知,将错误日志参数等信息以邮件方式发送给相关开发人员

2 目前cache使用的mysql,后续修改为redis来减缓mysql压力

4 后续添加按照组进行日志查看监控

5 按照组进行人员管理

最后附上地址

[GitHub地址](git@github.com:skeyboy/LogOnline.git)

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-11-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 大话swift 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 表结构:
  • 数据库连接、升级
  • 部署环境:
  • 用户登录(因为内部,自动注册人员
  • 日志大纲浏览
  • POSTMan压力测试
  • 后续改进
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档