Bu konu, Simple Log Service (SLS) etki alanına özel dilinin (DSL) temel sözdizimini açıklar.
Yorumlar
Sözdizimini açıklayan bir yorum eklemek için satırı diyez işareti (#) ile başlatın. Aşağıdaki kodda örnek gösterilmiştir:
# Set the default topic for logs. This is a comment at the beginning of a line.
e_set("__topic__", "access_log") # Set the default topic for logs. This is a comment at the end of a line.
Satır sonları
Bir fonksiyon çağrısının parametre listesi çok uzunsa satır sonu ekleyebilirsiniz.
Fonksiyon yapısında virgül (,) varsa satır sonunu doğrudan virgülden sonra ekleyebilirsiniz.
Bir dize çok uzunsa ve satır sonu gerekiyorsa ters eğik çizgi (\) kullanabilirsiniz.
Örnek:
e_set("__topic__", "v1",
"type", "v2", # When parameters are separated by commas (,), you can add a line break directly.
"length", 100)
e_set("__topic__", "this is a very long long long .........." \
"......long text") # Use a backslash (\) to add a line break.
Fonksiyon çağırma yöntemleri
-
Temel çağırma
e_set("abc", "xyz")NotVeri dönüştürme ifadesi yazarken giriş parametrelerinin veri türleri ve sayısı fonksiyon sözdizimiyle eşleşmelidir.
-
Temel değişken parametre çağırma
str_replace(value, old [,new [,count] ])NotBir fonksiyonun parametreleri köşeli parantezler ([]) içeriyorsa parantez içindeki parametreler, örneğin
newvecount, isteğe bağlıdır. Bunları adlandırılmış parametre olarak çağıramazsınız; belirtilen sırayla çağırmalısınız.# Invalid examples str_replace("a-b-c", "-", new='%') str_replace("a-b-c", "-", new='%', count=1) # Valid examples str_replace("a-b-c", "-", '%') str_replace("a-b-c", "-", '%', 2) -
Adlandırılmış parametre çağırma
Varsayılan değeri olan parametre, adlandırılmış parametredir. Örneğin,
e_set("abc", "xyz", mode="fill")ifadesindekimodeadlandırılmış bir parametredir.Bazı fonksiyonlarda belirli koşullar altında adlandırılmış parametrelerin değerlerini geçirmeniz gerekir. Daha fazla bilgi için her fonksiyonun parametre açıklamasına bakın.
Adlandırılmış bir parametreye değer geçirmek için
mode=...biçimini kullanın.Birden fazla adlandırılmış parametre varsa bunları belirtme sırası önemli değildir. Örneğin,
e_csv("data", ["f1", "f2", "f3"], sep='#', quote="|")ifadesie_csv("data", ["f1", "f2", "f3"], quote="|", sep='#')ifadesiyle aynıdır.
NotBir fonksiyon çağrısında adlandırılmış parametreler her zaman adlandırılmamış parametrelerden sonra belirtilmelidir.
-
İç içe fonksiyon çağrıları
Bir fonksiyonun dönüş değerini başka bir fonksiyonda parametre olarak kullanabilirsiniz. Veri türlerinin tutarlı olduğundan emin olun. Örnek:
e_set("abc", v("xyz")) e_set("abc", str_lower(v("xyz"))) -
Değişken parametreler
Bazı fonksiyonlar değişken parametreleri destekler.
v("f1", ....)ifadesiv("f1", "f2", "f3")gibi daha fazla parametre geçirebileceğiniz anlamına gelir.Adlandırılmış parametreleri geçirdiğinizde bunları en sona yerleştirmelisiniz. Örnek:
v("f1", "f2", "f3", "f4", mode="fill").
İşleçler
-
Karşılaştırma işleçleri
SLS DSL'in standart modu
>, <, >=, <=, !=, and ==dahil olmak üzere karşılaştırma işleçlerini destekler. Aynı etkiyi elde etmek için SLS tarafından sağlanan karşılaştırma fonksiyonlarını da kullanabilirsiniz.-
Karşılaştırma işleçlerini doğrudan kullanma
# The following examples show how to use comparison operators. If the comparison result is True, the log is dropped. e_if(3 > 2, DROP) # If 3 is greater than 2, the log is dropped. e_if(3 < 2, DROP) # If 3 is less than 2, the log is dropped. e_if(3 >= 2, DROP) # If 3 is greater than or equal to 2, the log is dropped. e_if(3 <= 2, DROP) # If 3 is less than or equal to 2, the log is dropped. e_if(3 == 2, DROP) # If 3 is equal to 2, the log is dropped. e_if(3 != 2, DROP) # If 3 is not equal to 2, the log is dropped. e_if(1 < 2 < 3, DROP) # If 2 is greater than 1 and less than 3, the log is dropped. e_if(0 < ct_int(v('x')) < 100, DROP) # If the value of the x field is greater than 0 and less than 100, the log is dropped. -
SLS tarafından sağlanan karşılaştırma fonksiyonlarını kullanma
İşlem
Fonksiyon
Örnek
Eşittir==
op_eqop_eq(v("name"), "xiao ming")
Eşit değildir!=
op_neop_ne(v("name"), "xiao ming")
Büyüktür>
op_gtop_gt(ct_int(v("age")))
Büyük veya eşittir>=
op_geop_ge(ct_int(v("age")), 18)
Küçüktür<
op_ltop_lt(ct_int(v("age")), 18)
Küçük veya eşittir<=
op_leop_le(ct_int(v("age")), 18)
-
-
Mantıksal işleçler
SLS DSL'in standart modu and, or ve not mantıksal işleçlerini destekler. Aynı etkiyi elde etmek için SLS tarafından sağlanan mantıksal fonksiyonları da kullanabilirsiniz.
-
Mantıksal işleçleri doğrudan kullanma
# The following examples show how to use logical operators. If the result of the logical operation is True, the log is dropped. e_if(True and False, DROP) # The result is False. e_if(True or False, DROP) # The result is True. e_if(True and not False, DROP) # The result is True. e_if(3 > 2 and 1 < 3, DROP) # The result is True. e_if(ct_int(v('x')) > 100 or ct_int(v('y')) < 100, DROP) # The value of the x field is greater than 100 or the value of the y field is less than 100. -
SLS tarafından sağlanan mantıksal fonksiyonları kullanma
İşlem
Fonksiyon
Örnek
Mantıksal ANDand
op_andop_and(op_gt(v("age"), 18), op_lt(v("age"), 31))
Mantıksal ORor
op_orop_or(op_le(v("age"), 18), op_gt(v("age"), 65))
Mantıksal NOTnot
op_notop_not(op_gt(v("age"), 18))
-
-
Diğer işleçler
Aşağıdaki işleçler SLS DSL'in standart modunda doğrudan desteklenmez. SLS, aynı etkileri elde etmek için karşılık gelen fonksiyonlar sağlar.
Senaryoya dayalı işlemler
Fonksiyon
Örnek
Toplama+
op_addop_add(v("age"), 2)
Çıkarma-
op_subop_sub(v("age"), 2)
Çarpma*
op_mulop_mul(v("size"), 2)
Üs alma**
op_powop_pow(v("size"), 2)
Taban bölme//
op_div_floorop_div_floor(v("bytes"), 1024)
Modülüs işlemi%
op_modop_mod(v("age"), 10)
Negatif yapma-
op_negop_neg(v("profit"))
Varlık kontrolüin
op_inop_in(["pass", "ok"], v("result"))
Yokluk kontrolünot in
op_not_inop_not_in(["pass", "ok"], v("result"))
Dize dilimleme[ ...]
op_sliceop_slice(v("message"), 0, 20)Örneğin,
aalanının değerini 3600*6 olarak ayarlamak için:# * e_set("a", 3600 * 6) # Invalid e_set("a", op_mul(3600, 6)) # Valid # / e_set("bytes_kb", v("bytes") / 1024) # Invalid e_set("bytes_kb", op_div_floor(v("bytes"), 1024)) # Valid
True ve false değerlendirme
Bazı fonksiyonlar bir koşulu kabul eder ve bu koşulun değerine göre sonraki mantığı belirler. Koşul statik bir alan veya bir ifadenin dönüş değeri olabilir.
SLS DSL, herhangi bir veri türünün değerlendirilmesini destekler. Aşağıdaki tabloda farklı veri türleri için true ve false değerlerinin koşulları açıklanmaktadır.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Aşağıda bırakılan bir olay örneği gösterilmektedir:
e_if(True, DROP) # If the input is True, the event is dropped.
e_if(1, DROP) # If the input is 1, the event is dropped.
e_if(v("abc"), DROP) # If the abc field exists and is not empty, the event is dropped.
e_if(str_isdigit(v("abc")), DROP) # If the abc field exists and its content consists only of digits, the event is dropped.