func (l *MysqlLogMgr) getLogTableName() string { curDate := time.Now().Format("2006_01_02") tableName := "t_syslog_" + curDate if l.lastDate != curDate { db := l.session.Driver().(*sql.DB) oldTableName := "t_syslog_" + time.Now().AddDate(0, 1, 0).Format("2006_01_02") if _, err := db.Exec(fmt.Sprintf(drop_log_table, oldTableName)); err != nil { fmt.Println(err) } /* if _, err := db.Exec(fmt.Sprintf(drop_log_table, tableName)); err != nil { fmt.Println(err) } */ if _, err := db.Exec(fmt.Sprintf(create_log_table_sql, tableName)); err != nil { fmt.Println(err) } l.lastDate = curDate } return tableName }
func (l *MysqlLogMgr) writeDB() { l.waitGroup.AddOne() defer l.waitGroup.Done() exitNotify := l.waitGroup.ExitNotify() db := l.session.Driver().(*sql.DB) for { select { case <-exitNotify: return case values := <-l.logSql: values = strings.TrimRight(values, ",") insertSql := fmt.Sprintf("insert delayed %v values", l.getLogTableName()) + values if _, err := db.Exec(insertSql); err != nil { fmt.Println(insertSql, err) } } } }