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
响应大概是0.4s左右
1 邮件通知,将错误日志参数等信息以邮件方式发送给相关开发人员
2 目前cache使用的mysql,后续修改为redis来减缓mysql压力
4 后续添加按照组进行日志查看监控
5 按照组进行人员管理
最后附上地址
[GitHub地址](git@github.com:skeyboy/LogOnline.git)